﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MaskedTextBox;
using MTextBox = MaskedTextBox.MaskedTextBox;

namespace QuanLyPhongKham
{
    public partial class ucMain : BaseUserControl
    {
        private DAL.AccessData m;
        private string sql = "", user = "", s_mabn = "", s_mmyy = "", s_makp_loc = "", s_menu = "", s_makp = "", s_ngaykham = "";
        private bool bTiepdon = false, bEdit = false;
        private long l_maql = 0;
        private decimal d_dongia = 0, d_tienthuoc = 0;
        private int i_userid = 0, i_hen = 0;
        private DataSet ds = new DataSet();
        private DataSet dsxml = new DataSet();
        private DataTable dtpk = new DataTable();
        private DataTable dtgiavp = new DataTable();
        private DataTable dtviettat = new DataTable();
        private DataTable dtbs = new DataTable();


        public ucMain()
        {
            InitializeComponent();
            load_tieude();
            this.Title = "Khám Bệnh";
        }
        private void lTen_DoubleClick(object sender, EventArgs e)
        {
            ucAbout f = new ucAbout(pForm.Location.X,pForm.Location.Y,lTen.Text,m);
            //fix:
            //f.ShowDialog();
            load_tieude();
        }
        private void QuanLyPhongKham_Load(object sender, EventArgs e)
        {
            var f = this.FormMain.FormLogin;

            if (f.i_userid != 0)
            {
                m = f.m;
                user = m.user;
                if (m.get_data("select * from " + user + ".dmgiavp where id=-9999").Tables[0].Rows.Count == 0)
                {
                    m.execute_data("insert into " + user + ".dmgiavp(id,ten,dongia,form,readonly,pttt) values(-9999,'Tiền khám bệnh',0,'',1,0)");
                }
                if (m.get_data("select * from " + user + ".dmgiavp where id=-9998").Tables[0].Rows.Count == 0)
                {
                    m.execute_data("insert into " + user + ".dmgiavp(id,ten,dongia,form,readonly,pttt) values(-9998,'Tiền thuốc',0,'',1,0)");
                }
                s_makp_loc = f.s_makp.Trim(',').Replace(",", "','");
                s_makp = f.s_makp.Trim(',');
                s_menu = f.s_menu;
                i_userid = f.i_userid;
                this.Text = "NIKSOFT - Quản Lý Phòng Khám <" + f.s_hoten + ">";
                bTiepdon = s_menu.IndexOf("c01") != -1;
                listDsbn.Visible = true;
                if (bTiepdon)
                {
                    gCaclankham.Text = "Các lần đăng ký khám";
                    gDausinhton.Visible = false;
                    gCaclankham.Location = new Point(2, 2);
                    pTiepdon.Visible = true;
                    pDscho.Visible = false;
                    tVienphi.Visible = true; tTienich.Visible = false;
                    tDonthuoc.Visible = false; tPttt.Visible = false;
                }
                else
                {
                    gCaclankham.Text = "Các lần khám bệnh";
                    pTiepdon.Visible = false; tTienich.Visible = false;
                    gDausinhton.Visible = true;
                    gKhambenh.Text = "Thông tin khám bệnh";
                    lChon.Text = "      THÔNG TIN KHÁM BỆNH";
                    butDiungthuoc.BackColor = Color.MintCream;
                    butDiungthuoc.ForeColor = Color.Black;
                }
                if (f.bAdmin)
                {
                    tTienich.Visible = true;
                    tVienphi.Visible = true;
                }
                s_mmyy = ngaylamviec.Text.Substring(3, 2) + ngaylamviec.Text.Substring(8, 2);
                if (!m.bMmyy(s_mmyy))
                {
                    m.tao_table(s_mmyy);
                    m.tao_function(s_mmyy);
                    m.upd_table(s_mmyy, i_userid);
                }
                load_tieude();
                loaddanhmuc();
                load_loaikham();
                load_viettat();
                if (!bTiepdon)
                {
                    pGiakham.Visible = true;
                    load_formnhap();
                }
                if (bTiepdon)
                {
                    pKhamvu.Visible = false; pKhampk.Visible = false; pGiakham.Visible = false; pTheodoitimthai.Visible = false; pSieuammau.Visible = false;
                    butIn.Visible = false; pKhamthai.Visible = false; pSieuam.Visible = false; pCanlamsang.Visible = false; pSieuam4D_12tuan.Visible = false; pSieuam_thai3thang.Visible = false;
                    lViettat.Visible = false; pSieuam_thainghen.Visible = false; pHiemmuon.Visible = false; pSieuam_pk.Visible = false; pEmpty.Visible = false;
                }
                else if (!bTiepdon) load_vv_mabn_chuakham();
                if (pDscho.Visible) pDscho.Focus();
                else mabn1.Focus();
                ngaykham.Text = m.ngayhienhanh_server;
                giokham.Text = m.giohienhanh_server;
                empty_text();
                cboGioitinh.SelectedIndex = 1;
                cbonn.SelectedIndex = 1;
                ena_doituong(false);
                ena_nuoc(false);
            }
            else { 
                //consider:
                Application.Exit(); 
            }
        }
        private void load_loaikham()
        {
            dtgiavp = m.get_data("select * from " + user + ".dmgiavp where pttt=0 and id not in(-9999,-9998)").Tables[0];
            DataRow r1 = dtgiavp.NewRow();
            r1["id"] = 0;
            r1["ten"] = "";
            r1["dongia"] = 0;
            r1["form"] = "";
            r1["readonly"] = 0;
            r1["pttt"] = 0;
            dtgiavp.Rows.InsertAt(r1, 0);
            dtgiavp.AcceptChanges();
            cboLoaikham.DataSource = dtgiavp;
        }
        private void load_viettat()
        {
            dtviettat = m.get_data("select * from " + user + ".dmviettat").Tables[0];
        }
        private void load_formnhap()
        {
            string s_form="";
            bEdit = false; pSieuam_thai3thang.Visible = false; pSieuam4D_12tuan.Visible = false;
            pKhampk.Visible = false; pSieuam_thainghen.Visible = false; pSieuam_pk.Visible = false; pTheodoitimthai.Visible = false; pSieuammau.Visible = false;
            pKhamvu.Visible = false; pKhamthai.Visible = false; pSieuam.Visible = false; pHiemmuon.Visible = false; pCanlamsang.Visible = false;
            DataRow rr = m.getrowbyid(dtgiavp, "id=" + long.Parse(cboLoaikham.SelectedValue.ToString())); 
            if (rr != null)
            {
                s_form = rr["form"].ToString();
                d_dongia = decimal.Parse(rr["dongia"].ToString());
            }
            switch (s_form)
            {
                case "pKhamvu": pKhamvu.Visible = true; if (l_maql != 0) load_khamvu(); break;
                case "pKhampk": pKhampk.Visible = true; if (l_maql != 0) load_khampk(); break;
                case "pKhamthai": pKhamthai.Visible = true; if (l_maql != 0) load_khamthai(); break;
                case "pHiemmuon": pHiemmuon.Visible = true; if (l_maql != 0) load_hiemmuon(); break;
                case "pSieuam": pSieuam.Visible = true; if (l_maql != 0) load_sieuam4d(); break;
                case "pSieuam_tn": pSieuam_thainghen.Visible = true; if (l_maql != 0) load_sieuam_thainghen(); break;
                case "pSieuam_th": pSieuam_thai3thang.Visible = true; if (l_maql != 0) load_sieuam_thai3thang(); break;
                case "pSieuam_pk": pSieuam_pk.Visible = true; if (l_maql != 0) load_sieuam_pk(); break;
                case "pCLS1": pCanlamsang.Visible = true; lTenCLS.Text = "Phiến đồ âm đạo :"; if (l_maql != 0) load_canlamsang(); break;
                case "pCLS2": pCanlamsang.Visible = true; lTenCLS.Text = "Soi tươi :"; if (l_maql != 0) load_canlamsang(); break;
                case "pCLS3": pCanlamsang.Visible = true; lTenCLS.Text = "Soi cổ tử cung :"; if (l_maql != 0) load_canlamsang(); break;
                case "pCLS4": pCanlamsang.Visible = true; lTenCLS.Text = "Đặt thuốc :"; if (l_maql != 0) load_canlamsang(); break;
                case "pTheodoi": pTheodoitimthai.Visible = true; if (l_maql != 0) load_theodoitimthai(); break;
                case "pSieuam4D": pSieuammau.Visible = true; if (l_maql != 0) load_sieuammau(); break;
                case "pSieuam_12": pSieuam4D_12tuan.Visible = true; if (l_maql != 0) load_sieuam_4d_12tuan(); break;
            }
        }
        private void load_tieude()
        {
            if (System.IO.File.Exists(@"..\..\..\xml\thongso.xml"))
            {
                dsxml.ReadXml(@"..\..\..\xml\thongso.xml");
                lTen.Text = dsxml.Tables[0].Rows[0]["c1"].ToString().ToUpper();
                lDiachi.Text = dsxml.Tables[0].Rows[0]["c2"].ToString();
            }
        }
        #region LoadDanhMuc
        private void loaddanhmuc()
        {
            cbonn.DisplayMember = "TENNN";
            cbonn.ValueMember = "MANN";
            load_btdnn();

            cboQuoctich.DisplayMember = "TEN";
            cboQuoctich.ValueMember = "MA";
            cboQuoctich.DataSource = m.get_data("select * from " + user + ".dmquocgia order by ma").Tables[0];
            cboQuoctich.SelectedValue = "VN";
            maqt.Text = "VN";

            cboDantoc.DataSource = m.get_data("select * from "+user+".btddt order by dantoc").Tables[0];
            cboDantoc.ValueMember = "madantoc";
            cboDantoc.DisplayMember = "dantoc";
            cboDantoc.SelectedValue = "25";
            madt.Text = "25";
            ena_nuoc(false);

            cboTinh.DisplayMember = "TENTT";
            cboTinh.ValueMember = "MATT";
            cboTinh.DataSource = m.get_data("select * from " + user + ".btdtt order by matt").Tables[0];
            //cboTinh.SelectedValue = m.Mabv.Substring(0, 3);

            cboQuan.DisplayMember = "TENQUAN";
            cboQuan.ValueMember = "MAQU";
            load_quan();

            cbopxa.DisplayMember = "TENPXA";
            cbopxa.ValueMember = "MAPHUONGXA";
            load_pxa();

            load_phongkham();  
            cboPhongkham.ValueMember = "MAKP";
            cboPhongkham.DisplayMember = "TENKP";
            cboPhongkham.DataSource = dtpk;

            cboDoituong.ValueMember = "MADOITUONG";
            cboDoituong.DisplayMember = "DOITUONG";
            cboDoituong.DataSource = m.get_data("select * from " + user + ".doituong order by madoituong").Tables[0];

            dtbs = m.get_data("select * from " + user + ".dmbs order by ma").Tables[0];
            cboBacsi.DisplayMember = "HOTEN";
            cboBacsi.ValueMember = "MA";
            cboBacsi.DataSource = dtbs;
            bacsikham.DisplayMember = "HOTEN";
            bacsikham.ValueMember = "MA";
            bacsikham.DataSource = dtbs;
            cboLoaikham.DisplayMember = "TEN";
            cboLoaikham.ValueMember = "ID";
        }
        private void load_phongkham()
        {
            sql = "select * from " + user + ".btdkp where makp in('" + s_makp_loc + "') order by makp";
            dtpk = m.get_data(sql).Tables[0];
        }
        private void load_quan()
        {
            cboQuan.DataSource = m.get_data("select * from " + user + ".btdquan where matt='" + cboTinh.SelectedValue.ToString() + "'" + " order by maqu").Tables[0];
        }

        private void load_pxa()
        {
            cbopxa.DataSource = m.get_data("select * from " + user + ".btdpxa where maqu='" + cboQuan.SelectedValue.ToString() + "'" + " order by maphuongxa").Tables[0];
        }
        private void load_btdnn()
        {
            cbonn.DataSource = m.get_data("select * from " + user + ".btdnn order by mann").Tables[0];
        }
        #endregion

        private void QuanLyPhongKham_SizeChanged(object sender, EventArgs e)
        {
            bNut.Location = new Point((this.Width - 800) / 2 + 65, 2);
            if(!gDausinhton.Visible) gCaclankham.Height = 432 + (this.Height - 600);
        }

        private void mabn_KeyDown(object sender, KeyEventArgs e)
            {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    ComboBox cb = (ComboBox)sender;
                    if (cb.SelectedIndex == -1) cb.SelectedIndex = 0;
                    SendKeys.Send("{Tab}{F4}");
                }
                catch { SendKeys.Send("{Tab}{F4}"); }
            }
        }

        private void bBaocao_Click(object sender, EventArgs e)
        {

        }

        private void toolStrip5_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        #region Ena_Object
        private void ena_nuoc(bool ena)
        {
            maqt.Enabled = ena;
            cboQuoctich.Enabled = ena;
        }
        private void ena_object(bool ena)
        {
            mabn1.Enabled = ena;
            mabn2.Enabled = ena;
            hoten.Enabled = ena;
            cboGioitinh.Enabled = ena;
            ngaysinh.Enabled = ena;
            namsinh.Enabled = ena;
            tuoi.Enabled = ena;
            cboTuoi.Enabled = ena;
            mann.Enabled = ena;
            cbonn.Enabled = ena;
            madt.Enabled = ena;
            cboDantoc.Enabled = ena;
            cboTinh.Enabled = ena;
            cboQuan.Enabled = ena;
            cbopxa.Enabled = ena;
            diachi.Enabled = ena;
            noilamviec.Enabled = ena;
            ngaykham.Enabled = ena;
            giokham.Enabled = ena;
            cboPhongkham.Enabled = ena;
            madoituong.Enabled = ena;
            cboDoituong.Enabled = ena;
            sothe.Enabled = ena;
            trieuchung.Enabled = ena;
            chandoan.Enabled = ena;
            mabs.Enabled = ena;
            cboBacsi.Enabled = ena;
            mach.Enabled = ena;
            nhietdo.Enabled = ena;
            huyetap.Enabled = ena;
            nhiptho.Enabled = ena;
            cannang.Enabled = ena;
            listDsbn.Enabled = ena;
            giakham.Enabled = ena;
            tienthuoc.Enabled = ena;
            sttkham.Enabled = ena;
        }
        private void empty_text()
        {
            mabn1.Text = DateTime.Now.Year.ToString().Substring(2);
            mabn2.Text = "";
            hoten.Text = "";
            //cboGioitinh.SelectedIndex = -1;
            namsinh.Text = "";
            ngaysinh.Text = "";
            tuoi.Text = "";
            //cboTuoi.SelectedIndex = -1;
            //mann.Text = "";
            diachi.Text = "";
            //matt.Text = "";
            //cboTinh.SelectedIndex = -1;
            //maquan1.Text = "";
            //maquan2.Text = "";
            //cboQuan.SelectedIndex = -1;
            //mapxa1.Text = "";
            //mapxa2.Text = "";
            //cbopxa.SelectedIndex = -1;
            noilamviec.Text = "";
            cboPhongkham.SelectedIndex = -1;
            madoituong.Text = "";
            cboDoituong.SelectedIndex = -1;
            sothe.Text = "";
            denngay.Text = "";
            trieuchung.Text = "";
            lydo.Text = "";
            sttkham.Text = "";
            chandoan.Text = "";
            mabs.Text = "";
            cboBacsi.SelectedIndex = -1;
            mach.Text = "";
            nhietdo.Text = "";
            huyetap.Text = "";
            nhiptho.Text = "";
            cannang.Text = "";
            cboQuoctich.SelectedValue = "VN";
            dienthoai.Text = "";
            didong.Text = "";
            coquan.Text = "";
            treeView1.Nodes.Clear();
        }
        private void empty_khamvu()
        {
            vutrai.Text = "";
            vuphai.Text = "";
            xetnghiem.Text = "";
            ketluan.Text = "";
        }
        private void empty_theodoitimthai()
        {
            tdt_conco.Text = "";
            tdt_nhiptim.Text = "";
            tdt_ketluan.Text = "";
        }
        private void ena_theodoitimthai(bool ena)
        {
            tdt_conco.Enabled = ena;
            tdt_nhiptim.Enabled = ena;
            tdt_ketluan.Enabled = ena;
        }
        private void ena_sieuammau(bool ena)
        {
            sam_dmron.Enabled = ena;
            sam_dmtc_p.Enabled = ena;
            sam_dmtc_t.Enabled = ena;
            sam_dmnao.Enabled = ena;
            sam_chisonao.Enabled = ena;
            sam_slthai.Enabled = ena;
            sam_timthai.Enabled = ena;
            sam_cudongthai.Enabled = ena;
            sam_duongkinh.Enabled = ena;
            sam_xuongdui.Enabled = ena;
            sam_rau.Enabled = ena;
            sam_oi.Enabled = ena;
        }
        private void empty_sieuammau()
        {
            sam_dmron.Text = "";
            sam_dmtc_p.Text = "";
            sam_dmtc_t.Text = "";
            sam_dmnao.Text = "";
            sam_chisonao.Text = "";
            sam_slthai.Text = "";
            sam_timthai.Text = "";
            sam_cudongthai.Text = "";
            sam_duongkinh.Text = "";
            sam_xuongdui.Text = "";
            sam_rau.Text = "";
            sam_oi.Text = "";
        }
        private void empty_canlamsang()
        {
            cls_ten.Text = "";
            cls_ketluan.Text = "";
        }
        private void ena_canlamsang(bool ena)
        {
            cls_ten.Enabled = ena;
            cls_ketluan.Enabled = ena;
        }
        private void empty_hiemmuon()
        {
            hm_toantrang.Text = ""; hm_tucung.Text = "";
            hm_khamvu.Text = ""; hm_cotucung.Text = "";
            hm_xetnghiem.Text = ""; hm_phanphu.Text = "";
            hm_ketluan.Text = "";
        }
        private void ena_hiemmuon(bool ena)
        {
            hm_toantrang.Enabled = ena; hm_tucung.Enabled = ena;
            hm_khamvu.Enabled = ena; hm_cotucung.Enabled = ena;
            hm_xetnghiem.Enabled = ena; hm_phanphu.Enabled = ena;
            hm_ketluan.Enabled = ena;
        }
        private void ena_sieuampk(bool ena)
        {
            sapk_tucung.Enabled = ena;
            sapk_phanphu_p.Enabled = ena; sapk_phanphu_t.Enabled = ena;
            sapk_tieukhung.Enabled = ena; sapk_khac.Enabled = ena;
            sapk_ketluan.Enabled = ena;
        }
        private void empty_sieuampk()
        {
            sapk_tucung.Text="";
            sapk_phanphu_p.Text=""; sapk_phanphu_t.Text="";
            sapk_tieukhung.Text=""; sapk_khac.Text="";
            sapk_ketluan.Text="";
        }
        private void empty_khampk()
        {
            p1.Text = ""; p2.Text = ""; p3.Text = ""; p4.Text = "";
            tiensu.Text = ""; 
            phanphu.Text = ""; obung.Text = ""; soicotc.Text = "";
            cotucung.Text = ""; khac.Text = ""; soituoi.Text = "";
            ketluan.Text = ""; tiensu.Text = ""; kinhnguyet.Text = ""; datvong.Text = "";
        }
        private void empty_khamthai()
        {
            p1_kt.Text= ""; p2_kt.Text= ""; p3_kt.Text= ""; p4_kt.Text= ""; tiensu_kt.Text= ""; ngaykc_kt.Text= "";
            huyetap_kt.Text=""; timphoi_kt.Text= ""; phune.Text= ""; benhkhac.Text= "";
            slthai.Text= ""; ngoithai.Text= ""; sodothai.Text= ""; timthai.Text= ""; trongluongthai.Text= ""; ngaysinhdk.Text= "";
            xnmau.Text = ""; xnntieu.Text = ""; sodokhac.Text = ""; ketluan_kt.Text = ""; ngoithai.SelectedIndex = -1;
        }
        private void empty_sieuamthai()
        {
            sat_tucung.Text = ""; sat_phanphu.Text = "";
            sat_buongtucung.Text = ""; sat_slthai.Text = ""; sat_sodothai.Text = "";
            sat_rauthai.Text = ""; sat_khac.Text = ""; sat_oithai.Text = "";
            sat_ketluan.Text = ""; sat_timthai.Text = ""; sat_cudongthai.Text = "";
        }
        private void empty_sieuamthai_3thang()
        {
            satt_tucung.Text = ""; satt_slthai.Text = "";
            satt_timthai.Text = ""; satt_cudongthai.Text = ""; satt_duongkinh.Text = "";
            satt_xuongdui.Text = ""; satt_trungbinhbung.Text = ""; satt_chuvibung.Text = "";
            satt_rau.Text = ""; satt_oi.Text = ""; satt_trongluongthai.Text = ""; satt_dudoansinh.Text = "";
            satt_ketluan.Text = "";
        }
        private void empty_sieuam4d_12tuan()
        {
            sa_12_tucung.Text = ""; sa_12_phanphu.Text = ""; sa_12_slthai.Text = "";
            sa_12_timthai.Text = ""; sa_12_cudongthai.Text = ""; sa_12_duongkinh.Text = "";
            sa_12_xuongdui.Text = ""; sa_12_vomso.Text = ""; sa_12_cotsong.Text = "";
            sa_12_chan.Text = ""; sa_12_tay.Text = ""; sa_12_longnguc.Text = ""; sa_12_phoi.Text = "";
            sa_12_khoangsang.Text = ""; sa_12_obung.Text = ""; sa_12_vitrirau.Text = ""; sa_12_nuocoi.Text = "";
            sa_12_trongluong.Text = ""; sa_12_tuoi_KCC.Text = ""; sa_12_tuoi_sieuam.Text = "";
            sa_12_dudoansinh.Text = ""; sa_12_nhanxet.Text = "";
            sa_12_ketluan.Text = "";
        }
        private void ena_sieuam4d_12tuan(bool ena)
        {
            sa_12_tucung.Enabled = ena; sa_12_phanphu.Enabled = ena; sa_12_slthai.Enabled = ena;
            sa_12_timthai.Enabled = ena; sa_12_cudongthai.Enabled = ena; sa_12_duongkinh.Enabled = ena;
            sa_12_xuongdui.Enabled = ena; sa_12_vomso.Enabled = ena; sa_12_cotsong.Enabled = ena;
            sa_12_chan.Enabled = ena; sa_12_tay.Enabled = ena; sa_12_longnguc.Enabled = ena; sa_12_phoi.Enabled = ena;
            sa_12_khoangsang.Enabled = ena; sa_12_obung.Enabled = ena; sa_12_vitrirau.Enabled = ena; sa_12_nuocoi.Enabled = ena;
            sa_12_trongluong.Enabled = ena; sa_12_tuoi_KCC.Enabled = ena; sa_12_tuoi_sieuam.Enabled = ena;
            sa_12_dudoansinh.Enabled = ena; sa_12_nhanxet.Enabled = ena;
            sa_12_ketluan.Enabled = ena;
        }
        private void ena_sieuamthai(bool ena)
        {
            sat_tucung.Enabled = ena; sat_phanphu.Enabled = ena;
            sat_buongtucung.Enabled = ena; sat_slthai.Enabled = ena; sat_sodothai.Enabled = ena;
            sat_rauthai.Enabled = ena; sat_khac.Enabled = ena; sat_oithai.Enabled = ena;
            sat_ketluan.Enabled = ena; sat_timthai.Enabled = ena; sat_cudongthai.Enabled = ena;
        }
        private void ena_sieuamthai_3thang(bool ena)
        {
            satt_tucung.Enabled = ena; satt_slthai.Enabled = ena;
            satt_timthai.Enabled = ena; satt_cudongthai.Enabled = ena; satt_duongkinh.Enabled = ena;
            satt_xuongdui.Enabled = ena; satt_trungbinhbung.Enabled = ena; satt_chuvibung.Enabled = ena;
            satt_rau.Enabled = ena; satt_oi.Enabled = ena; satt_trongluongthai.Enabled = ena; satt_dudoansinh.Enabled = ena;
            satt_ketluan.Enabled = ena;
        }
        private void ena_doituong(bool ena)
        {
            sothe.Enabled = ena;
            denngay.Enabled = ena;
        }
        private void ena_button(bool ena)
        {
            mabn1.Enabled = !ena;
            butMoi.Enabled = ena;
            butLuu.Enabled = !ena;
            butBoqua.Enabled = !ena;
        }
        #endregion

        #region SetValues
        private void cbonn_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                mann.Text = cbonn.SelectedValue.ToString();
            }
            catch { }
        }

        private void cboDantoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                madt.Text = cboDantoc.SelectedValue.ToString();
                if (cboDantoc.SelectedValue.ToString() == "55") ena_nuoc(true);
            }
            catch { }
        }

        private void cboQuoctich_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                maqt.Text = cboQuoctich.SelectedValue.ToString();
            }
            catch { }
        }

        private void cboTinh_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                matt.Text = cboTinh.SelectedValue.ToString();
                load_quan();
            }
            catch { }
        }

        private void cboQuan_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                maquan1.Text = cboQuan.SelectedValue.ToString().Substring(0, 3);
                maquan2.Text = cboQuan.SelectedValue.ToString().Substring(3);
                load_pxa();
            }
            catch { }
        }

        private void cbopxa_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                mapxa1.Text = cbopxa.SelectedValue.ToString().Substring(0, 5);
                mapxa2.Text = cbopxa.SelectedValue.ToString().Substring(5);
            }
            catch { }
        }

        private void madt_Validated(object sender, EventArgs e)
        {
            try 
            {
                madt.Text = madt.Text.Trim();
                cboDantoc.SelectedValue = madt.Text.Trim();
            }
            catch { }
        }

        private void mann_Validated(object sender, EventArgs e)
        {
            try { cbonn.SelectedValue = mann.Text; }
            catch { }
        }

        private void cboDoituong_SelectedIndexChanged(object sender, EventArgs e)
        {
            try 
            { 
                madoituong.Text = cboDoituong.SelectedValue.ToString();
                if (madoituong.Text == "1") ena_doituong(true);
                else
                {
                    ena_doituong(false);
                    denngay.Text = "";
                }
            }
            catch { }
        }

        private void madoituong_Validated(object sender, EventArgs e)
        {
            try { cboDoituong.SelectedValue = madoituong.Text; }
            catch { }
        }

        private void cboBacsi_SelectedIndexChanged(object sender, EventArgs e)
        {
            try { mabs.Text = cboBacsi.SelectedValue.ToString(); }
            catch { }
        }

        private void mabs_Validated(object sender, EventArgs e)
        {
            try { cboBacsi.SelectedValue = mabs.Text; }
            catch { }
        }
        #endregion

        private void butLuu_Click(object sender, EventArgs e)
        {
            if (bTiepdon)
            {
                if (kiemtra())
                {
                    if (sttkham.Text == "")
                    {
                        MessageBox.Show("Nhập số thứ tự khám!", DAL.AccessData.Msg);
                        sttkham.Focus();
                        return;
                    }
                    if (!m.upd_btdbn(s_mabn, hoten.Text, (ngaysinh.Text.Trim().Length == 10) ? ngaysinh.Text : "", namsinh.Text, cboGioitinh.SelectedIndex, mann.Text, madt.Text, diachi.Text, noilamviec.Text, matt.Text, maquan1.Text + maquan2.Text, mapxa1.Text + mapxa2.Text, i_userid))
                    {
                        MessageBox.Show("Không cập nhật được thông tin hành chính!", DAL.AccessData.Msg);
                        return;
                    }
                    string s_ngayvv = ngaykham.Text + " " + giokham.Text;
                    long maql = 0;
                    maql = l_maql;
                    if (l_maql == 0) l_maql = m.get_maql;
                    //if (sttkham.Text == "")
                    //{
                    //    sttkham.Text = m.get_sttkham(ngaykham.Text).ToString();
                    //}
                    if (ngaykham.Text == m.ngayhienhanh_server) i_hen = 0;
                    else if (m.bNgay(ngaykham.Text, m.ngayhienhanh_server)) i_hen = 1;
                    else i_hen = 0;
                    if (!m.upd_benhanpk(s_mabn, l_maql, cboPhongkham.SelectedValue.ToString(), s_ngayvv, m.get_lanthu_vv(s_mabn), tuoi.Text + cboTuoi.SelectedIndex.ToString(), 2, sothe.Enabled ? sothe.Text : "", (denngay.Enabled) ? denngay.Text : "", lydo.Text, chandoan.Text, "", "", mabs.Text, i_userid, (maql == 0) ? 0 : 1, int.Parse(sttkham.Text), i_hen))
                    {
                        MessageBox.Show("Không cập nhật được thông tin khám bệnh!", DAL.AccessData.Msg);
                        return;
                    }
                    if (dienthoai.Text.Trim() != "" || didong.Text.Trim() != "" || coquan.Text.Trim() != "")
                    {
                        m.upd_dienthoai(s_mabn, dienthoai.Text, didong.Text, coquan.Text);
                    }
                    ena_object(false);
                    ena_button(true);
                    butMoi.Focus();
                }
            }
            else 
            {
                if (decimal.Parse(giakham.Text) < d_dongia)
                {
                    MessageBox.Show("Giá này nhỏ hơn giá đã nhập.\nGiá đã nhập trước của BN này là : " + d_dongia.ToString("###,###,##0"), DAL.AccessData.Msg);
                    giakham.Focus(); return;
                }
                if (kiemtra())
                {
                    if (giakham.Text == "" || giakham.Text == "0")
                    {
                        MessageBox.Show("Nhập tiền khám bệnh!",DAL.AccessData.Msg);
                        giakham.Focus();
                        return;
                    }
                    l_maql = (l_maql == 0) ? m.get_maql_ngayvv(s_mabn, s_ngaykham.Substring(0,10)) : l_maql;
                    if (kiemtra_thutien())
                    {
                        MessageBox.Show("Đợt khám này BN đã đóng tiền, không được chỉnh sửa!\nMuốn khám lần khám khác, BN phải qua tiếp đón nhập lần khám mới.\nHoặc:\nHuỷ hoá đơn đã thu dưới phòng viện phí để có thể chỉnh sửa thông tin!", DAL.AccessData.Msg);
                        return;
                    }
                    if (cboLoaikham.SelectedIndex != 0)
                    {
                        if (pKhamvu.Visible)
                        {
                            if (!kiemtra_detail(pKhamvu)) return;
                            if (!m.upd_khamvu(s_mabn, l_maql, s_makp, s_ngaykham, 1, vutrai.Text.Trim(), vuphai.Text.Trim(), xetnghiem.Text.Trim(), ketluan.Text.Trim(), bacsikham.SelectedValue.ToString()))
                            {
                                MessageBox.Show("Không cập nhật được thông tin khám vú!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pCanlamsang.Visible)
                        {
                            if (!kiemtra_detail(pCanlamsang)) return;
                            if (!m.upd_canlamsang(s_mabn, l_maql, s_makp, s_ngaykham, 1, cls_ten.Text.Trim(), cls_ketluan.Text.Trim(), bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin " + lTenCLS.Text.ToLower() + "!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pTheodoitimthai.Visible)
                        {
                            if (!kiemtra_detail(pTheodoitimthai)) return;
                            if (!m.upd_theodoitimthai(s_mabn, l_maql, s_makp, s_ngaykham, 1, tdt_conco.Text.Trim(), tdt_nhiptim.Text, tdt_ketluan.Text.Trim(), bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin theo dõi tim thai !", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuammau.Visible)
                        {
                            if (!kiemtra_detail(pSieuammau)) return;
                            if (!m.upd_sieuammau(s_mabn, l_maql, s_makp, s_ngaykham, 1, "", sam_dmron.Text, sam_dmtc_p.Text.Trim(), sam_dmtc_t.Text, sam_dmnao.Text, sam_chisonao.Text, sam_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid, sam_slthai.Text, sam_timthai.Text, sam_cudongthai.Text, sam_duongkinh.Text, sam_xuongdui.Text, sam_rau.Text, sam_oi.Text))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm màu 4D !", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pKhampk.Visible)
                        {
                            if (!kiemtra_detail(pKhampk)) return;
                            if (!m.upd_khampk(s_mabn, l_maql, s_makp, s_ngaykham, 1, p1.Text + p2.Text + p3.Text + p4.Text, tiensu.Text, 10, 1, 1, phanphu.Text, cotucung.Text, obung.Text, khac.Text, soituoi.Text, soicotc.Text, ketluan_pk.Text, bacsikham.SelectedValue.ToString(), i_userid, tucung.Text, kinhcuoi.Text, kinhnguyet.Text, datvong.Text))
                            {
                                MessageBox.Show("Không cập nhật được thông tin khám phụ khoa!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pKhamthai.Visible)
                        {
                            if (!kiemtra_detail(pKhamthai)) return;
                            if (slthai.Text.Trim() == "")
                            {
                                MessageBox.Show("Nhập số lượng thai!", DAL.AccessData.Msg);
                                slthai.Focus();
                                return;
                            }
                            string nkc = "";
                            if (ngaykc_kt.Text.Trim() != "/  /") nkc = ngaykc_kt.Text;
                            if (ngaysinhdk.Text.Trim() == "/  /")
                            {
                                MessageBox.Show("Nhập ngày sinh dự kiến!", DAL.AccessData.Msg);
                                ngaysinhdk.Focus();
                                return;
                            } 
                            if (!m.upd_khamthai(s_mabn, l_maql, s_makp, s_ngaykham, m.get_lanthu_kham(s_mabn, "ba_khamthai"), p1_kt.Text + p2_kt.Text + p3_kt.Text + p4_kt.Text, nkc, tiensu_kt.Text, huyetap_kt.Text, timphoi_kt.Text, phune.Text, benhkhac.Text, slthai.Text, ngoithai.SelectedIndex, sodothai.Text, timthai.Text, trongluongthai.Text, ngaysinhdk.Text, xnmau.Text, xnntieu.Text, sodokhac.Text, ketluan_kt.Text, bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin khám thai!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuam.Visible)
                        {
                            if (!kiemtra_detail(pSieuam)) return;
                            if (sa_sothai.Text.Trim() == "")
                            {
                                MessageBox.Show("Nhập số thai trong tử cung!", DAL.AccessData.Msg);
                                sa_sothai.Focus(); return;
                            }
                            if (!m.upd_sieuam4d(s_mabn, l_maql, s_makp, s_ngaykham, 1, sa_tucung.Text, sa_phanphu.Text, sa_sothai.Text, sa_cudongthai.Text, sa_duongkinh.Text, sa_chieudaidui.Text, sa_chieudaichan.Text, sa_chuvidau.Text, sa_chuvibung.Text, sa_kcachnhancau.Text, sa_chieudaimong.Text, "",
                                sa_khoangsang.Text, sa_chieudaidui.Text, sa_bohopso.Text, sa_cautrucnao.Text, sa_hexuong1.Text, sa_hexuong2.Text, sa_hexuong3.Text, sa_thanhbung.Text, sa_bangquang.Text, sa_haithan.Text, sa_vitrirau.Text, sa_dotruongthanh.Text, sa_dodayrau.Text, sa_mepbanhrau.Text, sa_soluongnuocoi.Text, sa_tuoithai_kcc.Text,
                                sa_tuoithai_sa.Text, sa_dudoansinh.Text, sa_trongluongthai.Text, sa_nhanxet.Text, sa_longnguc.Text, sa_haiphoi.Text, sa_dodongam.Text, sa_tuidaday.Text, "", sa_dmron.Text, sa_dmtucung_p.Text, sa_dmtucung_t.Text, sa_dmnao.Text, sa_chisoron.Text, sa_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid, sa_timthai.Text))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm 4D!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuam_thainghen.Visible)
                        {
                            if (!kiemtra_detail(pSieuam_thainghen)) return;
                            if (sat_slthai.Text.Trim() == "")
                            {
                                MessageBox.Show("Nhập số thai trong tử cung!", DAL.AccessData.Msg);
                                sat_slthai.Focus(); return;
                            }
                            string nsdk = "";
                            if (sat_dudoansinh.Text.Trim() != "/  /") nsdk = sat_dudoansinh.Text;
                            if (!m.upd_sieuamthai(s_mabn, l_maql, s_makp, s_ngaykham, m.get_lanthu_kham(s_mabn, "ba_sieuamthai"), sat_tucung.Text, sat_phanphu.Text, sat_buongtucung.Text, sat_slthai.Text, sat_sodothai.Text, sat_rauthai.Text, sat_oithai.Text, sat_khac.Text, sat_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid, sat_timthai.Text, sat_cudongthai.Text, nsdk))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm thai nghén!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuam_thai3thang.Visible)
                        {
                            if (!kiemtra_detail(pSieuam_thai3thang)) return;
                            if (satt_slthai.Text.Trim() == "")
                            {
                                MessageBox.Show("Nhập số thai trong tử cung!", DAL.AccessData.Msg);
                                satt_slthai.Focus(); return;
                            }
                            string nsdk = "";
                            if (satt_dudoansinh.Text.Trim() != "/  /") nsdk = satt_dudoansinh.Text;
                            if (!m.upd_sieuamthai_3thang(s_mabn, l_maql, s_makp, s_ngaykham, m.get_lanthu_kham(s_mabn, "ba_sieuamthai_3thang"), satt_tucung.Text, satt_slthai.Text, satt_timthai.Text, satt_cudongthai.Text, satt_duongkinh.Text, satt_xuongdui.Text, satt_trungbinhbung.Text, satt_chuvibung.Text, satt_rau.Text, satt_oi.Text, satt_trongluongthai.Text, nsdk, satt_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm thai!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuam4D_12tuan.Visible)
                        {
                            if (!kiemtra_detail(pSieuam4D_12tuan)) return;
                            if (sa_12_slthai.Text.Trim() == "")
                            {
                                MessageBox.Show("Nhập số thai trong tử cung!", DAL.AccessData.Msg);
                                sa_12_slthai.Focus(); return;
                            }
                            string nsdk = "";
                            if (sa_12_dudoansinh.Text.Trim() != "/  /") nsdk = sa_12_dudoansinh.Text;
                            if (!m.upd_sieuam4d_12tuan(s_mabn, l_maql, s_makp, s_ngaykham, m.get_lanthu_kham(s_mabn, "ba_sieuam4d_12tuan"), sa_12_tucung.Text, sa_12_phanphu.Text, sa_12_slthai.Text, sa_12_timthai.Text, sa_12_cudongthai.Text, sa_12_duongkinh.Text, sa_12_xuongdui.Text, sa_12_vomso.Text, sa_12_cotsong.Text, sa_12_chan.Text, sa_12_tay.Text, sa_12_longnguc.Text, sa_12_phoi.Text, sa_12_khoangsang.Text, sa_12_obung.Text, sa_12_vitrirau.Text, sa_12_nuocoi.Text, sa_12_tuoi_KCC.Text, sa_12_tuoi_sieuam.Text, sa_12_trongluong.Text, nsdk, sa_12_nhanxet.Text, sa_12_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm 4D trên 12 tuần!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pHiemmuon.Visible)
                        {
                            if (!kiemtra_detail(pHiemmuon)) return;
                            if (!m.upd_hiemmuon(s_mabn, l_maql, s_makp, s_ngaykham, 1, hm_toantrang.Text, hm_khamvu.Text, hm_tucung.Text, hm_cotucung.Text, hm_phanphu.Text, hm_xetnghiem.Text, hm_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin khám hiếm muộn!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                        if (pSieuam_pk.Visible)
                        {
                            if (!kiemtra_detail(pSieuam_pk)) return;
                            if (!m.upd_sieuampk(s_mabn, l_maql, s_makp, s_ngaykham, 1, "", sapk_tucung.Text, sapk_phanphu_p.Text, sapk_phanphu_t.Text, sapk_tieukhung.Text, sapk_khac.Text, sapk_ketluan.Text, bacsikham.SelectedValue.ToString(), i_userid))
                            {
                                MessageBox.Show("Không cập nhật được thông tin siêu âm phụ khoa!", DAL.AccessData.Msg);
                                return;
                            }
                        }
                    }
                    if (mach.Text != "" || nhietdo.Text != "" || huyetap.Text != "" || nhiptho.Text != "" || cannang.Text != "")
                    {
                        m.upd_dausinhton(l_maql, (mach.Text == "") ? 0 : decimal.Parse(mach.Text), (nhietdo.Text == "") ? 0 : decimal.Parse(nhietdo.Text), huyetap.Text, (nhiptho.Text == "") ? 0 : decimal.Parse(nhiptho.Text), (cannang.Text == "") ? 0 : decimal.Parse(cannang.Text));
                    }
                    //Chi dinh : chi lay 1 gia
                    string s_form = "";
                    foreach (DataRow r1 in m.get_data("select ghichu,paid from " + user + ".v_chidinh where stt=1 and id=" + l_maql).Tables[0].Rows)
                    {
                        s_form = r1["ghichu"].ToString();
                    }
                    DataRow r2 = m.getrowbyid(dtgiavp, "id=" + int.Parse(cboLoaikham.SelectedValue.ToString()));
                    if (r2 != null)
                    {
                        if (s_form.IndexOf(r2["form"].ToString()) == -1)
                            s_form += r2["form"].ToString() + ",";
                    }
                    m.upd_chidinh(l_maql, 1, s_mabn, l_maql, s_ngaykham, s_makp, 2, -9999, 1, decimal.Parse(giakham.Text), 0, 0, i_userid, 0, s_form,bacsikham.SelectedValue.ToString());
                    if(tienthuoc.Text!="") m.upd_chidinh(l_maql, 2, s_mabn, l_maql, s_ngaykham, s_makp, 2, -9998, 1, decimal.Parse(tienthuoc.Text), 0, 0, i_userid, 0, "", bacsikham.SelectedValue.ToString());
                    //
                    //if (!bEdit)
                    //{
                    //    long l_id = m.get_capid;
                    //    long idchidinh = m.get_capid_chidinh;
                    //    m.upd_chidinh(l_id, 1, s_mabn, l_maql, s_ngaykham, s_makp, 2, int.Parse(cboLoaikham.SelectedValue.ToString()), 1, decimal.Parse(giakham.Text), 0, 0, i_userid, idchidinh, cboLoaikham.Text);
                    //}
                    m.execute_data("update " + user + ".benhanpk set done=1, xutri='" + giakham.Text + "', chandoan='" + tienthuoc.Text + "', mabs='" + bacsikham.SelectedValue.ToString() + "' where maql=" + l_maql);
                    ena_object(false);
                    ena_button(true);
                    if (cboLoaikham.SelectedIndex != 0)
                    {
                        if (pKhamvu.Visible) ena_khamvu(false);
                        if (pKhampk.Visible) ena_khampk(false);
                        if (pKhamthai.Visible) ena_khamthai(false);
                        if (pSieuam.Visible) ena_sieuam4d(false);
                        if (pSieuam_thainghen.Visible) ena_sieuamthai(false);
                        if (pSieuam_thai3thang.Visible) ena_sieuamthai_3thang(false);
                        if (pHiemmuon.Visible) ena_hiemmuon(false);
                        if (pSieuam_pk.Visible) ena_sieuampk(false);
                        if (pCanlamsang.Visible) ena_canlamsang(false);
                        if (pTheodoitimthai.Visible) ena_theodoitimthai(false);
                        if (pSieuammau.Visible) ena_sieuammau(false);
                        if (pSieuam4D_12tuan.Visible) ena_sieuam4d_12tuan(false);
                    }
                    butMoi.Focus();
                }
            }
        }
        private bool kiemtra_detail(Panel p)
        {
            int idem = 0, iControl = 0;
            foreach (Control ctr in p.Controls)
            {
                if (ctr.GetType() != typeof(Label) && ctr.GetType() != typeof(Panel) && ctr.GetType() != typeof(GroupBox))
                {
                    iControl++;
                    if (ctr.GetType() == typeof(TextBox))
                    {
                        TextBox txt = (TextBox)ctr;
                        if (txt.Text == "") idem++;
                    }
                    else if (ctr.GetType() == typeof(ComboBox))
                    {
                        ComboBox cbo = (ComboBox)ctr;
                        if (cbo.SelectedIndex == -1) idem++;
                    }
                    //else if (ctr.GetType() == typeof(MaskedTextBox))
                    //{
                    //    MaskedTextBox mask = (MaskedTextBox)ctr;
                    //    if (mask.Text.Trim() == "/  /") idem++;
                    //}
                }
            }
            if (iControl == idem && iControl != 0)
            {
                MessageBox.Show("Vui lòng nhập kết quả khám của bệnh nhân!", DAL.AccessData.Msg);
                return false;
            }
            return true;
        }
        private void ena_khamvu(bool ena)
        {
            vutrai.Enabled = ena;
            vuphai.Enabled = ena; xetnghiem.Enabled = ena; ketluan.Enabled = ena;
        }
        private void ena_khampk(bool ena)
        {
            p1.Enabled = ena; p2.Enabled = ena; p3.Enabled = ena; p4.Enabled = ena; tiensu.Enabled = ena;
            kinhnguyet.Enabled = ena; kinhcuoi.Enabled = ena; tucung.Enabled = ena; tiensu.Enabled = ena;
            phanphu.Enabled = ena; cotucung.Enabled = ena; khac.Enabled = ena; obung.Enabled = ena; soicotc.Enabled = ena; soituoi.Enabled = ena;
            ketluan_pk.Enabled = ena; datvong.Enabled = ena;
        }
        private void ena_khamthai(bool ena)
        {
            p1_kt.Enabled = ena; p2_kt.Enabled = ena; p3_kt.Enabled = ena; p4_kt.Enabled = ena; tiensu_kt.Enabled = ena; ngaykc_kt.Enabled = ena;
            huyetap_kt.Enabled = ena; timphoi_kt.Enabled = ena; phune.Enabled = ena; benhkhac.Enabled = ena;
            slthai.Enabled = ena; ngoithai.Enabled = ena; sodothai.Enabled = ena; timthai.Enabled = ena; trongluongthai.Enabled = ena; ngaysinhdk.Enabled = ena;
            xnmau.Enabled = ena; xnntieu.Enabled = ena; sodokhac.Enabled = ena; ketluan_kt.Enabled = ena;
        }
        private void ena_sieuam4d(bool ena)
        {
            sa_tucung.Enabled = ena; sa_phanphu.Enabled = ena; sa_sothai.Enabled = ena; sa_cudongthai.Enabled = ena; sa_duongkinh.Enabled = ena; sa_chieudaidui.Enabled = ena;
            sa_chieudaichan.Enabled = ena; sa_chuvidau.Enabled = ena; sa_chuvibung.Enabled = ena; sa_kcachnhancau.Enabled = ena; sa_chieudaimong.Enabled = ena; 
            sa_khoangsang.Enabled = ena; sa_chieudaimui.Enabled = ena; sa_bohopso.Enabled = ena; sa_cautrucnao.Enabled = ena; sa_hexuong1.Enabled = ena; sa_hexuong2.Enabled = ena; sa_hexuong3.Enabled = ena;
            sa_thanhbung.Enabled = ena; sa_bangquang.Enabled = ena; sa_haithan.Enabled = ena; sa_vitrirau.Enabled = ena; sa_dotruongthanh.Enabled = ena; sa_dodayrau.Enabled = ena; sa_mepbanhrau.Enabled = ena;
            sa_soluongnuocoi.Enabled = ena; sa_tuoithai_kcc.Enabled = ena; sa_tuoithai_sa.Enabled = ena; sa_dudoansinh.Enabled = ena; sa_trongluongthai.Enabled = ena; sa_nhanxet.Enabled = ena; sa_ketluan.Enabled = ena;
            sa_longnguc.Enabled = ena; sa_haiphoi.Enabled = ena;
            sa_tuidaday.Enabled = ena;
            sa_dodongam.Enabled = ena;
            sa_dmron.Enabled = ena;
            sa_dmtucung_p.Enabled = ena;
            sa_dmtucung_t.Enabled = ena;
            sa_dmnao.Enabled = ena;
            sa_chisoron.Enabled = ena;
            sa_timthai.Enabled = ena;
        }
        private void empty_sieuam4d()
        {
            sa_tucung.Text= ""; sa_phanphu.Text= ""; sa_sothai.Text= ""; sa_cudongthai.Text= ""; sa_duongkinh.Text= ""; sa_chieudaidui.Text= "";
            sa_chieudaichan.Text= ""; sa_chuvidau.Text= ""; sa_chuvibung.Text= ""; sa_kcachnhancau.Text= ""; sa_chieudaimong.Text= "";
            sa_khoangsang.Text= ""; sa_chieudaimui.Text=""; sa_bohopso.Text= ""; sa_cautrucnao.Text= ""; sa_hexuong1.Text= ""; sa_hexuong2.Text= ""; sa_hexuong3.Text= "";
            sa_thanhbung.Text= ""; sa_bangquang.Text= ""; sa_haithan.Text= ""; sa_vitrirau.Text= ""; sa_dotruongthanh.Text= ""; sa_dodayrau.Text= ""; sa_mepbanhrau.Text= "";
            sa_soluongnuocoi.Text= ""; sa_tuoithai_kcc.Text= ""; sa_tuoithai_sa.Text= ""; sa_dudoansinh.Text= ""; sa_trongluongthai.Text= ""; sa_nhanxet.Text= ""; sa_ketluan.Text= "";
            sa_longnguc.Text = ""; sa_haiphoi.Text = "";
            sa_tuidaday.Text = "";
            sa_dodongam.Text = "";
            sa_dmron.Text = "";
            sa_dmtucung_p.Text = "";
            sa_dmtucung_t.Text = "";
            sa_dmnao.Text = "";
            sa_chisoron.Text = "";
            sa_timthai.Text = "";
        }
        private void butKetthuc_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn kết thúc chương trình không?",DAL.AccessData.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Information,MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                //fix:
                //this.Close();

            }
        }

        private void butMoi_Click(object sender, EventArgs e)
        {
            giakham.Text = "0";
            tienthuoc.Text = "0";
            s_mabn = "";
            l_maql = 0;
            d_dongia = 0; d_tienthuoc = 0;
            if (!bTiepdon)
            {
                lChon.Text = "      THÔNG TIN KHÁM BỆNH";
                butDiungthuoc.BackColor = Color.MintCream;
                butDiungthuoc.ForeColor = Color.Black;
                load_vv_mabn_chuakham();
            }
            ena_button(false);
            ena_object(true);  
            empty_text();
            empty_khamvu();
            empty_canlamsang();
            empty_theodoitimthai();
            empty_sieuammau();
            empty_khampk();
            empty_khamthai();
            empty_sieuam4d();
            empty_sieuamthai();
            empty_sieuamthai_3thang();
            empty_sieuam4d_12tuan();
            empty_hiemmuon();
            empty_sieuampk();
            if (pKhamvu.Visible)
            {
                ena_khamvu(true);
            }
            if (pCanlamsang.Visible)
            {
                ena_canlamsang(true);
            }
            if (pTheodoitimthai.Visible)
            {
                ena_theodoitimthai(true);
            }
            if (pSieuammau.Visible)
            {
                ena_sieuammau(true);
            }
            if (pKhampk.Visible)
            {
                ena_khampk(true); 
            }
            if (pKhamthai.Visible)
            {
                ena_khamthai(true); 
            }
            if (pSieuam.Visible)
            {
                ena_sieuam4d(true); 
            }
            if (pSieuam_thainghen.Visible)
            {
                ena_sieuamthai(true);
            }
            if (pSieuam4D_12tuan.Visible)
            {
                ena_sieuam4d_12tuan(true);
            }
            if (pSieuam_thai3thang.Visible)
            {
                ena_sieuamthai_3thang(true);
            }
            if (pHiemmuon.Visible)
            {
                ena_hiemmuon(true);
            }
            if (pSieuam_pk.Visible)
            {
                ena_sieuampk(true);
            }
            cboGioitinh.SelectedIndex = 1;
            ngaykham.Text = m.ngayhienhanh_server;
            giokham.Text = m.giohienhanh_server;
            mabn1.Text = DateTime.Now.Year.ToString().Substring(2);
            mabn1.Focus();
        }

        private void butBoqua_Click(object sender, EventArgs e)
        {
            s_mabn = "";
            l_maql = 0;
            if(!bTiepdon) load_vv_mabn_chuakham();
            ena_button(true);
            ena_object(false);
            empty_text();
            if (pKhamvu.Visible) empty_khamvu();
            if (pKhampk.Visible) empty_khampk();
            if (pKhamthai.Visible) empty_khamthai();
            if (pSieuam.Visible) empty_sieuam4d();
            if (pSieuam4D_12tuan.Visible) empty_sieuam4d_12tuan();
            if (pSieuam_thainghen.Visible) empty_sieuamthai();
            if (pSieuam_thai3thang.Visible) empty_sieuamthai_3thang();
            if (pHiemmuon.Visible) empty_hiemmuon();
            if (pCanlamsang.Visible) empty_canlamsang();
            if (pTheodoitimthai.Visible) empty_theodoitimthai();
        }
        private bool load_mabn()
        {
            bool ret = false;
            foreach (DataRow r in m.get_data("select * from "+user+".btdbn where mabn='" + s_mabn + "'").Tables[0].Rows)
            {
                hoten.Text = r["hoten"].ToString();
                namsinh.Text = r["namsinh"].ToString();
                if (r["ngaysinh"].ToString() != "")
                {
                    ngaysinh.Text = m.DateToString("dd/MM/yyyy", DateTime.Parse(r["ngaysinh"].ToString()));
                    string tuoivao = m.Tuoivao("", ngaysinh.Text);
                    tuoi.Text = tuoivao.Substring(2).PadLeft(3, '0');
                    cboTuoi.SelectedIndex = int.Parse(tuoivao.Substring(0, 1));
                }
                else
                {
                    tuoi.Text = Convert.ToString(DateTime.Now.Year - int.Parse(namsinh.Text)).PadLeft(3, '0');
                    cboTuoi.SelectedIndex = 0;
                }
                cboGioitinh.SelectedIndex = int.Parse(r["phai"].ToString());
                mann.Text = r["mann"].ToString();
                cbonn.SelectedValue = r["mann"].ToString();
                cboDantoc.SelectedValue = r["madantoc"].ToString();
                if (r["madantoc"].ToString() == "55") ena_nuoc(true);
                else ena_nuoc(false);
                cboQuoctich.SelectedValue = r["manuoc"].ToString();
                diachi.Text = r["diachi"].ToString();
                noilamviec.Text = r["cholam"].ToString();
                cboTinh.SelectedValue = r["matt"].ToString();
                load_quan();
                cboQuan.SelectedValue = r["maqu"].ToString();
                load_pxa();
                cbopxa.SelectedValue = r["maphuongxa"].ToString();
                foreach (DataRow nr in m.get_data("select * from " + user + ".dienthoai where mabn='" + s_mabn + "'").Tables[0].Rows)
                {
                    dienthoai.Text = nr["dienthoai"].ToString();
                    didong.Text = nr["didong"].ToString();
                    coquan.Text = nr["coquan"].ToString();
                }
                ret = true;
                break;
            }
            if (ret)
            {
                load_diungthuoc();
                load_treeView();
            }
            return ret;
        }
        private void load_diungthuoc()
        {
            string s_thuocdiung = "";
            int iMucdo_Max = 1;
            sql = "select a.mucdo,b.tenhc from " + user + ".d_diungthuoc a,"+user+".d_dmhoatchat b where a.mahc=b.mahc and a.mabn='" + s_mabn + "'";
            foreach (DataRow r in m.get_data(sql).Tables[0].Rows)
            {
                iMucdo_Max = (int.Parse(r["mucdo"].ToString())>iMucdo_Max)?int.Parse(r["mucdo"].ToString()):iMucdo_Max;
                s_thuocdiung+=r["tenhc"].ToString()+", ";
            }
            if (s_thuocdiung != "")
            {
                switch (iMucdo_Max)
                {
                    case 1: butDiungthuoc.BackColor = Color.Pink; break;
                    case 2: butDiungthuoc.BackColor = Color.Yellow; break;
                    case 3: butDiungthuoc.BackColor = Color.Red;
                        butDiungthuoc.ForeColor = Color.Blue;
                        break;
                }
                lChon.Text += "-->Dị ứng hoạt chất thuốc :" + s_thuocdiung.Trim().Trim(',');
            }
        }
        private void load_treeView()
        {
            treeView1.Nodes.Clear();
            TreeNode node;
            string s_dk = (bTiepdon) ? " " : " and done=1";
            foreach (DataRow r in m.get_data("select to_char(ngay,'dd/mm/yyyy hh24:mi') as ngay,chandoan,maql,to_char(ngay,'yyyymmddhh24mi') as yymmdd from " + user + ".benhanpk where mabn='" + s_mabn + "' "+s_dk+" and makp in('"+s_makp_loc+"')").Tables[0].Select("true", "yymmdd desc"))
            {
                node = treeView1.Nodes.Add(r["ngay"].ToString());
                node.Tag = r["maql"].ToString();
            }
        }

        private bool load_vv_mabn()
        {
            l_maql = 0;
            d_dongia = 0; d_tienthuoc = 0;
            sql = "select to_char(a.ngay,'dd/mm/yyyy hh24:mi') as ngay,a.* from " + user + ".benhanpk a where a.mabn='" + s_mabn + "' and to_char(a.ngay,'dd/mm/yyyy')='" + m.ngayhienhanh_server.Substring(0, 10) + "' and a.makp in('"+s_makp_loc+"') order by a.ngay desc";
            foreach (DataRow r in m.get_data(sql).Tables[0].Rows)
            {
                l_maql = long.Parse(r["maql"].ToString());
                //ngaykham.Text = m.DateToString("dd/MM/yyyy", DateTime.Parse(r["ngay"].ToString()));
                //giokham.Text = m.DateToString("HH:mm", DateTime.Parse(r["ngay"].ToString()));
                //s_ngaykham = ngaykham.Text + " " + giokham.Text;
                s_ngaykham = r["ngay"].ToString();
                cboPhongkham.SelectedValue = r["makp"].ToString();
                //cboDoituong.SelectedValue = r["madoituong"].ToString();
                s_makp = r["makp"].ToString();
                bacsikham.SelectedValue = r["mabs"].ToString();
                //madoituong.Text = r["madoituong"].ToString();
                //if (r["madoituong"].ToString() == "1")
                //{
                //    ena_doituong(true);
                //    sothe.Text = r["sothe"].ToString();
                //    denngay.Text = m.DateToString("dd/MM/yyyy", DateTime.Parse(r["denngay"].ToString()));
                //}
                lydo.Text = r["trieuchung"].ToString();
                if(r["xutri"].ToString()!="")
                    d_dongia = decimal.Parse(r["xutri"].ToString());
                if (r["chandoan"].ToString() != "")
                    d_tienthuoc = decimal.Parse(r["chandoan"].ToString());
                giakham.Text = d_dongia.ToString("###,###,##0");
                tienthuoc.Text = d_tienthuoc.ToString("###,###,##0");
                break;
            }
            return l_maql != 0;
        }
        private void load_vv_mabn_chuakham()
        {
            sql = "select a.stt,b.hoten,decode(a.hen,1,'?','') as hen,a.maql,a.mabn,to_char(a.ngay,'dd/mm/yyyy hh24:mi') as sort from " + user + ".benhanpk a," + user + ".btdbn b where a.mabn=b.mabn and to_char(a.ngay,'dd/mm/yyyy')='" + m.ngayhienhanh_server.Substring(0, 10) + "' and a.done = 0 and a.makp in('" + s_makp_loc + "') order by a.stt";
            listDsbn.DisplayMember = "HOTEN";
            listDsbn.ValueMember = "MAQL";
            listDsbn.DataSource = m.get_data(sql).Tables[0];
            listDsbn.ColumnWidths[0] = 30;
            listDsbn.ColumnWidths[1] = 260;
            listDsbn.ColumnWidths[2] = 0;
            listDsbn.SetSensive(2, "?", Color.Red);
            pDscho.Visible = listDsbn.Items.Count > 0;
            if (!tTienich.Visible)
            {
                pDscho.Height = 381 + (this.Height - 600);
                pDscho.Location = new System.Drawing.Point(1, 61);
            }
        }
        private bool kiemtra()
        {
            if (mabn1.Text == "" ||  mabn2.Text=="")
            {
                MessageBox.Show("Vui lòng nhập mã bệnh nhân!", DAL.AccessData.Msg); mabn1.Focus(); return false;
            }
            if (hoten.Text == "")
            {
                MessageBox.Show("Vui lòng nhập họ tên bệnh nhân!", DAL.AccessData.Msg); hoten.Focus(); return false;
            }
            if (namsinh.Text == "")
            {
                MessageBox.Show("Vui lòng nhập năm sinh bệnh nhân!", DAL.AccessData.Msg); namsinh.Focus(); return false;
            }
            
            return true;
        }

        private void ngaykham_Validated(object sender, EventArgs e)
        {
            if(!m.bNgay(ngaykham.Text.Trim()))
            {
                MessageBox.Show("Ngày không hợp lệ! Vui lòng nhập lại theo định dạng : dd/MM/yyyy", DAL.AccessData.Msg);
                ngaykham.Focus();
                return;
            }
        }

        private void giokham_Validated(object sender, EventArgs e)
        {
            l_maql = 0;
            if (!m.bGio(giokham.Text.Trim()))
            {
                MessageBox.Show("Giờ không hợp lệ! Vui lòng nhập lại theo định dạng HH:SS", DAL.AccessData.Msg);
                giokham.Focus();
                return;
            }
            string s_ngayvv = ngaykham.Text + " " + giokham.Text;
            l_maql = m.get_maql_ngay_gio_vv(s_mabn, s_ngayvv);
            if (l_maql != 0)
            {
                load_vv_maql(true);
            }
            else 
            {
                l_maql = m.get_maql_ngayvv(s_mabn, ngaykham.Text);
                if (l_maql != 0)
                {
                    cboPhongkham.SelectedValue = m.get_makp(l_maql);
                    if (MessageBox.Show("Trong ngày người bệnh này đã khám tại phòng khám [" + cboPhongkham.Text.ToUpper() + "] !\nBạn có muốn xem lại không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        load_vv_maql(false);
                        chandoan.Focus();
                    }
                    else
                    {
                        l_maql = 0;
                        mabs.Text = "";
                        cboBacsi.SelectedIndex = -1;
                        cboPhongkham.DroppedDown = true;
                        lydo.Text = "";
                    }
                }
            }
        }
        private void load_vv_maql(bool skip)
        {
            foreach (DataRow r in m.get_data("select to_char(a.ngay,'dd/mm/yyyy hh24:mi') as ngay,a.* from " + user + ".benhanpk a where a.maql=" + l_maql).Tables[0].Rows)
            {
                if (!skip)
                {
                    ngaykham.Text = r["ngay"].ToString().Substring(0,10);
                    giokham.Text = r["ngay"].ToString().Substring(11);
                }
                s_mabn = r["mabn"].ToString();
                mabn1.Text = s_mabn.Substring(0, 2);
                mabn2.Text = s_mabn.Substring(2);
                sttkham.Text = r["stt"].ToString();
                i_hen = int.Parse(r["hen"].ToString());
                s_ngaykham = r["ngay"].ToString();
                s_makp = r["makp"].ToString();
                bacsikham.SelectedValue = r["mabs"].ToString();
                cboDoituong.SelectedValue = r["madoituong"].ToString();
                madoituong.Text = r["madoituong"].ToString();
                cboPhongkham.SelectedValue = r["makp"].ToString();
                trieuchung.Text = r["trieuchung"].ToString();
                //chandoan.Text = r["chandoan"].ToString();
                mabs.Text = r["mabs"].ToString();
                cboBacsi.SelectedValue = r["mabs"].ToString();
                //if (r["madoituong"].ToString() == "1")
                //{
                //    ena_doituong(true);
                //    sothe.Text = r["sothe"].ToString();
                //    denngay.Text = m.DateToString("dd/MM/yyyy",DateTime.Parse(r["denngay"].ToString()));
                //}
                string stuoi = r["tuoivao"].ToString();
                if (stuoi.Length == 4)
                {
                    tuoi.Text = stuoi.Substring(0, 3);
                    cboTuoi.SelectedIndex = Math.Min(int.Parse(stuoi.Substring(3, 1)), 3);
                }
                mach.Text = ""; nhietdo.Text = ""; huyetap.Text = ""; nhiptho.Text = ""; cannang.Text = "";
                foreach (DataRow r1 in m.get_data("select * from " + user + ".dausinhton where maql=" + l_maql).Tables[0].Rows)
                {
                    mach.Text = r1["mach"].ToString();
                    nhietdo.Text = r1["nhietdo"].ToString();
                    huyetap.Text = r1["huyetap"].ToString();
                    nhiptho.Text = r1["nhiptho"].ToString();
                    cannang.Text = r1["cannang"].ToString();
                    break;
                }
                break;
            }
        }
        
        private void sothe_Validated(object sender, EventArgs e)
        {
            if (sothe.Enabled && sothe.Text.Trim() == "")
                sothe.Focus();
        }

        private void denngay_Validated(object sender, EventArgs e)
        {
            if (denngay.Text.Trim() == "__/__/____" || denngay.Text.Trim() == "")
            {
                MessageBox.Show("Vui lòng nhập ngày hết hạn của thẻ BHYT!");
                denngay.Focus();
                return;
            }
            if (!m.bNgay(denngay.Text))
            {
                MessageBox.Show("Ngày hết hạn của thẻ BHYT không hợp lệ!");
                denngay.Focus();
                return;
            }
            if (!m.bNgay(denngay.Text, ngaykham.Text))
            {
                MessageBox.Show("Ngày hết hạn của thẻ BHYT không được nhỏ hơn ngày khám bệnh!");
                denngay.Focus();
                return;
            }
        }

        private void ngaysinh_Validated(object sender, EventArgs e)
        {
            if (ngaysinh.Text.Trim() != "" && ngaysinh.Text.Trim() != "/  /" && ngaysinh.Text.Trim().Length==10)
            {
                if (!m.bNgay(m.ngayhienhanh_server, ngaysinh.Text))
                {
                    MessageBox.Show("Ngày sinh không được lớn hơn ngày hiện tại!");
                    ngaysinh.Focus();
                    return;
                }
                if (!m.bNgay(ngaysinh.Text))
                {
                    MessageBox.Show("Ngày sinh không hợp lệ! Vui lòng nhập đúng định dạng : dd/MM/yyyy");
                    ngaysinh.Focus();
                    return;
                }
                try
                {
                    string tuoivao = m.Tuoivao("", ngaysinh.Text);
                    tuoi.Text = tuoivao.Substring(2).PadLeft(3, '0');
                    cboTuoi.SelectedIndex = int.Parse(tuoivao.Substring(0, 1));
                    namsinh.Text = ngaysinh.Text.Substring(6, 4);
                    if (tuoi.Text == "000")
                    {
                        MessageBox.Show("Yêu cầu nhập tuổi vào !", DAL.AccessData.Msg);
                        tuoi.Focus();
                        return;
                    }
                }
                catch { }
            }
        }

        private void hoten_Validated(object sender, EventArgs e)
        {
            //hoten.Text = m.ChangeCase(hoten.Text);
        }

        private void diachi_Validated(object sender, EventArgs e)
        {
            diachi.Text = m.ChangeCase(diachi.Text);
        }

        private void noilamviec_Validated(object sender, EventArgs e)
        {
            noilamviec.Text = m.ChangeCase(noilamviec.Text);
        }

        private void xutri_Validated(object sender, EventArgs e)
        {
            butLuu.Focus();
        }

        private void chandoan_Validated(object sender, EventArgs e)
        {
            if (chandoan.Text.Trim() == "")
            {
                butLuu.Focus();
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse || e.Action == TreeViewAction.ByKeyboard)
            {
                try
                {
                    string ngay = e.Node.FullPath.Trim();
                    l_maql = m.get_maql_ngay_gio_vv(s_mabn, ngay);
                    if (l_maql != 0)
                    {
                        ngaykham.Text = ngay.Substring(0,10);
                        giokham.Text = ngay.Substring(11);
                        load_vv_maql(true);
                        if(pKhamvu.Visible) load_khamvu();
                        if (pKhampk.Visible) load_khampk();
                        if (pKhamthai.Visible) load_khamthai();
                        if (pSieuam.Visible) load_sieuam4d();
                        if (pSieuam_thainghen.Visible) load_sieuam_thainghen();
                        if (pSieuam_thai3thang.Visible) load_sieuam_thai3thang();
                        if (pHiemmuon.Visible) load_hiemmuon();
                        if (pSieuam_pk.Visible) load_sieuam_pk();
                        if (pCanlamsang.Visible) load_canlamsang();
                        if (pTheodoitimthai.Visible) load_theodoitimthai();
                        if (pSieuammau.Visible) load_sieuammau();
                    }
                }
                catch { }
            }
        }
        private void load_khamvu()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_khamvu where maql=" + l_maql).Tables[0].Rows)
            {
                vutrai.Text = r["vutrai"].ToString();
                vuphai.Text = r["vuphai"].ToString();
                xetnghiem.Text = r["xetnghiem"].ToString();
                ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_canlamsang()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_canlamsang where maql=" + l_maql).Tables[0].Rows)
            {
                cls_ten.Text = r["ten"].ToString();
                cls_ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_theodoitimthai()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_theodoitimthai where maql=" + l_maql).Tables[0].Rows)
            {
                tdt_conco.Text =r["tanso"].ToString();
                tdt_ketluan.Text = r["nhiptim"].ToString();
                tdt_nhiptim.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuammau()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuammau where maql=" + l_maql).Tables[0].Rows)
            {
                sam_dmron.Text = r["dmron"].ToString();
                sam_dmtc_p.Text = r["dmtucung_p"].ToString();
                sam_dmtc_t.Text = r["dmtucung_t"].ToString();
                sam_dmnao.Text = r["dmnao"].ToString();
                sam_chisonao.Text = r["chisonao"].ToString();
                sam_ketluan.Text = r["ketluan"].ToString();
                sam_slthai.Text = r["slthai"].ToString();
                sam_timthai.Text = r["timthai"].ToString();
                sam_cudongthai.Text = r["cudongthai"].ToString();
                sam_duongkinh.Text = r["duongkinh"].ToString();
                sam_xuongdui.Text = r["xuongdui"].ToString();
                sam_rau.Text = r["rau"].ToString();
                sam_oi.Text = r["oi"].ToString();
                bEdit = true;
            }
        }
        private void load_hiemmuon()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_hiemmuon where maql=" + l_maql).Tables[0].Rows)
            {
                hm_toantrang.Text = r["toantrang"].ToString();
                hm_khamvu.Text = r["vu"].ToString();
                hm_tucung.Text = r["tucung"].ToString();
                hm_cotucung.Text = r["cotucung"].ToString();
                hm_phanphu.Text = r["phanphu"].ToString();
                hm_xetnghiem.Text = r["xetnghiem"].ToString();
                hm_ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuam_pk()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuampk where maql=" + l_maql).Tables[0].Rows)
            {
                sapk_tucung.Text = r["tucung"].ToString();
                sapk_phanphu_p.Text = r["phanphu_p"].ToString();
                sapk_phanphu_t.Text = r["phanphu_t"].ToString();
                sapk_tieukhung.Text = r["tieukhung"].ToString();
                sapk_khac.Text = r["khac"].ToString();
                sapk_ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_khampk()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_khampk where maql=" + l_maql).Tables[0].Rows)
            {
                p1.Text = r["para"].ToString().Substring(0, 2);
                p2.Text = r["para"].ToString().Substring(2, 2);
                p3.Text = r["para"].ToString().Substring(4, 2);
                p4.Text = r["para"].ToString().Substring(6, 2);
                tiensu.Text = r["tiensu"].ToString();
                kinhcuoi.Text = r["kinhcuoi"].ToString();
                kinhnguyet.Text = r["kinhnguyet"].ToString();
                tucung.Text = r["tucung"].ToString();
                phanphu.Text = r["phanphu"].ToString();
                cotucung.Text = r["cotucung"].ToString();
                obung.Text = r["obung"].ToString();
                khac.Text = r["khac"].ToString();
                soituoi.Text = r["soituoi"].ToString();
                soicotc.Text = r["soictc"].ToString();
                ketluan_pk.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_khamthai()
        {
            foreach (DataRow r in m.get_data("select to_char(a.dukiensinh,'dd/mm/yyyy') as dukiensinh,a.* from " + user + ".ba_khamthai a where a.maql=" + l_maql).Tables[0].Rows)
            {
                p1_kt.Text = r["para"].ToString().Substring(0, 2);
                p2_kt.Text = r["para"].ToString().Substring(2, 2);
                p3_kt.Text = r["para"].ToString().Substring(4, 2);
                p4_kt.Text = r["para"].ToString().Substring(6, 2);
                huyetap_kt.Text = r["huyetap"].ToString();
                tiensu_kt.Text = r["tiensu"].ToString();
                ngaykc_kt.Text = r["ngaykinhc"].ToString();
                timphoi_kt.Text = r["timphoi"].ToString();
                phune.Text= r["phune"].ToString();
                benhkhac.Text = r["khac"].ToString();
                slthai.Text = r["soluongthai"].ToString();
                ngoithai.SelectedIndex= int.Parse(r["ngoithai"].ToString());
                sodothai.Text = r["sodothai"].ToString();
                timthai.Text = r["timthai"].ToString();
                trongluongthai.Text = r["trongluongthai"].ToString();
                ngaysinhdk.Text = r["dukiensinh"].ToString();
                xnmau.Text = r["xnmau"].ToString();
                xnntieu.Text = r["xnnuoctieu"].ToString();
                sodokhac.Text = r["sodokhac"].ToString();
                ketluan_kt.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuam_thainghen()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuamthai where maql=" + l_maql).Tables[0].Rows)
            {
                sat_tucung.Text = r["tucung"].ToString();
                sat_phanphu.Text = r["phanphu"].ToString();
                sat_buongtucung.Text = r["buongtucung"].ToString();
                sat_slthai.Text = r["soluongthai"].ToString();
                sat_sodothai.Text = r["sodothai"].ToString();
                sat_rauthai.Text = r["rauthai"].ToString();
                sat_oithai.Text = r["oithai"].ToString();
                sat_khac.Text = r["khac"].ToString();
                sat_timthai.Text = r["timthai"].ToString();
                sat_cudongthai.Text = r["cudongthai"].ToString();
                sat_ketluan.Text = r["ketluan"].ToString();
                sat_dudoansinh.Text = r["dudoansinh"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuam_thai3thang()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuamthai_3thang where maql=" + l_maql).Tables[0].Rows)
            {
                satt_tucung.Text = r["tucung"].ToString();
                satt_slthai.Text = r["soluongthai"].ToString();
                satt_timthai.Text = r["timthai"].ToString();
                satt_cudongthai.Text = r["cudongthai"].ToString();
                satt_duongkinh.Text = r["duongkinh"].ToString();
                satt_xuongdui.Text = r["xuongdui"].ToString();
                satt_trungbinhbung.Text = r["trungbinhbung"].ToString();
                satt_chuvibung.Text = r["chuvibung"].ToString();
                satt_rau.Text = r["rauthai"].ToString();
                satt_oi.Text = r["oithai"].ToString();
                satt_trongluongthai.Text = r["trongluongthai"].ToString();
                satt_dudoansinh.Text = r["dudoansinh"].ToString();
                satt_ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuam_4d_12tuan()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuam4d_12tuan where maql=" + l_maql).Tables[0].Rows)
            {
                sa_12_tucung.Text = r["tucung"].ToString();
                sa_12_phanphu.Text = r["phanphu"].ToString();
                sa_12_slthai.Text = r["soluongthai"].ToString();
                sa_12_timthai.Text = r["timthai"].ToString();
                sa_12_cudongthai.Text = r["cudongthai"].ToString();
                sa_12_duongkinh.Text = r["duongkinh"].ToString();
                sa_12_xuongdui.Text = r["xuongdui"].ToString();
                sa_12_vomso.Text = r["vomso"].ToString();
                sa_12_cotsong.Text = r["cotsong"].ToString();
                sa_12_chan.Text = r["chan"].ToString();
                sa_12_tay.Text = r["tay"].ToString();
                sa_12_longnguc.Text = r["longnguc"].ToString();
                sa_12_phoi.Text = r["phoi"].ToString();
                sa_12_khoangsang.Text = r["khoangsang"].ToString();
                sa_12_obung.Text = r["obung"].ToString();
                sa_12_vitrirau.Text = r["vitrirau"].ToString();
                sa_12_nuocoi.Text = r["nuocoi"].ToString();
                sa_12_tuoi_KCC.Text = r["tuoithai_kcc"].ToString();
                sa_12_tuoi_sieuam.Text = r["tuoithai_sa"].ToString();
                sa_12_trongluong.Text = r["trongluongthai"].ToString();
                sa_12_dudoansinh.Text = r["dudoansinh"].ToString();
                sa_12_nhanxet.Text = r["nhanxet"].ToString();
                sa_12_ketluan.Text = r["ketluan"].ToString();
                bEdit = true;
            }
        }
        private void load_sieuam4d()
        {
            foreach (DataRow r in m.get_data("select * from " + user + ".ba_sieuam4d where maql=" + l_maql).Tables[0].Rows)
            {
                sa_tucung.Text = r["tucung"].ToString(); 
                sa_phanphu.Text = r["phanphu"].ToString(); 
                sa_sothai.Text = r["sothai"].ToString(); 
                sa_cudongthai.Text = r["cudongthai"].ToString(); 
                sa_duongkinh.Text = r["duongkinh"].ToString(); 
                sa_chieudaidui.Text = r["chieudaidui"].ToString();
                sa_chieudaichan.Text = r["chieudaichan"].ToString(); 
                sa_chuvidau.Text = r["chuvidau"].ToString(); 
                sa_chuvibung.Text = r["chuvibung"].ToString(); 
                sa_kcachnhancau.Text = r["kcachnhancau"].ToString(); 
                sa_chieudaimong.Text = r["chieudaimong"].ToString(); 
                sa_khoangsang.Text = r["khoangsang"].ToString(); 
                sa_chieudaimui.Text = r["chieudaimui"].ToString(); 
                sa_bohopso.Text = r["bohopso"].ToString(); 
                sa_cautrucnao.Text = r["cautrucnao"].ToString(); 
                sa_hexuong1.Text = r["hexuong1"].ToString(); 
                sa_hexuong2.Text = r["hexuong2"].ToString(); 
                sa_hexuong3.Text = r["hexuong3"].ToString();
                sa_thanhbung.Text = r["thanhbungtruoc"].ToString(); 
                sa_bangquang.Text = r["bangquang"].ToString();
                sa_haithan.Text = r["haithan"].ToString();
                sa_vitrirau.Text = r["vitrirau"].ToString(); 
                sa_dotruongthanh.Text = r["dotruongthanhrau"].ToString(); 
                sa_dodayrau.Text = r["dodayrau"].ToString(); 
                sa_mepbanhrau.Text = r["mepbanhrau"].ToString();
                sa_soluongnuocoi.Text = r["soluongnuocoi"].ToString(); 
                sa_tuoithai_kcc.Text = r["tuoithai_kcc"].ToString(); 
                sa_tuoithai_sa.Text = r["tuoithai_sa"].ToString(); 
                sa_dudoansinh.Text = r["dudoansinh"].ToString(); 
                sa_trongluongthai.Text = r["trongluongthai"].ToString(); 
                sa_nhanxet.Text = r["nhanxet"].ToString(); 
                sa_ketluan.Text = r["ketluan"].ToString();
                sa_tuidaday.Text = r["tuidaday"].ToString();
                sa_dodongam.Text = r["dodongtam"].ToString();
                sa_dmron.Text = r["dmron"].ToString();
                sa_dmtucung_p.Text = r["dmtucung_p"].ToString();
                sa_dmtucung_t.Text = r["dmtucung_t"].ToString();
                sa_dmnao.Text=r["dmnao"].ToString();
                sa_chisoron.Text = r["chisonao"].ToString();
                sa_timthai.Text = r["nhiptim"].ToString();
                bEdit = true;
            }
        }
        private void namsinh_Validated(object sender, EventArgs e)
        {
            if (namsinh.Text != "")
            {
                if (int.Parse(namsinh.Text) > DateTime.Now.Year)
                {
                    MessageBox.Show("Năm sinh không hợp lệ !", DAL.AccessData.Msg);
                    namsinh.Focus();
                    return;
                }
                if (namsinh.Text.Length < 4) namsinh.Text = Convert.ToString(DateTime.Now.Year - int.Parse(namsinh.Text));
                tuoi.Text = Convert.ToString(DateTime.Now.Year - int.Parse(namsinh.Text)).PadLeft(3, '0');
                cboTuoi.SelectedIndex = 0;
                if (!load_tim_mabn())
                {
                    mann.Focus();
                }
            }
        }
        private bool load_tim_mabn()
        {
            s_mabn = mabn1.Text + mabn2.Text;
            DataTable dt = m.get_Timmabn(hoten.Text.Trim().ToUpper(), namsinh.Text, s_mabn).Tables[0];
            if (dt.Rows.Count > 0)
            {
                frmTimbenhnhan f = new frmTimbenhnhan(dt);
                f.ShowDialog();
                if (f.s_mabn != "")
                {
                    mabn1.Text = f.s_mabn.Substring(0, 2);
                    mabn2.Text = f.s_mabn.Substring(2, 6);
                    s_mabn = f.s_mabn.Substring(0, 2) + f.s_mabn.Substring(2, 6);
                    if (load_mabn()) ngaykham.Focus();
                    return true;
                }
            }
            return false;
        }
        private void tuoi_Validated(object sender, EventArgs e)
        {
            try
            {
                if (int.Parse(tuoi.Text) == 0) ngaysinh.Focus();
                else
                {
                    if (namsinh.Text == "")
                    {
                        namsinh.Text = (DateTime.Now.Year - int.Parse(tuoi.Text)).ToString();
                        cboTuoi.SelectedIndex = 0;
                        if (!load_tim_mabn())
                        {
                            mann.Focus();
                        }
                    }
                }
            }
            catch { ngaysinh.Focus(); }
        }

        private void MaskDigit(KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 8 || e.KeyChar == '/' || e.KeyChar == '.') e.Handled = false;
            else e.Handled = true;
        }

        private void mabn_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskDigit(e);
        }

        private void mabn2_Validated(object sender, EventArgs e)
        {
            if (mabn2.Text.Trim() == "")
            {
                if (bTiepdon)
                {
                    s_mabn = DateTime.Now.Year.ToString().Substring(2) + m.get_mabn_tudong(int.Parse(DateTime.Now.Year.ToString().Substring(2)), 1, true).ToString().PadLeft(6, '0');
                    mabn1.Text = s_mabn.Substring(0, 2);
                    mabn2.Text = s_mabn.Substring(2);
                }
            }
            else
            {
                mabn2.Text = mabn2.Text.PadLeft(6, '0');
                s_mabn = mabn1.Text + mabn2.Text;
            }
            if (load_mabn())
            {
                if(!load_vv_mabn())
                {
                    if (!bTiepdon)
                    {
                        MessageBox.Show("Bệnh nhân chưa qua tiếp đón!", DAL.AccessData.Msg); mabn1.Focus(); return;
                    }
                }
                if (bTiepdon)
                {
                    if (MessageBox.Show("Bạn có sửa thông tin hành chính không?", DAL.AccessData.Msg, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                    {
                        hoten.Focus();
                    }
                    else ngaykham.Focus();
                }
                else
                {
                    if (pKhamthai.Visible) p1_kt.Focus();
                    if (pKhampk.Visible) p1.Focus();
                    if (pKhamvu.Visible) vutrai.Focus();
                    if (pSieuam.Visible) sa_tucung.Focus();
                    if (pSieuam4D_12tuan.Visible) sa_12_tucung.Focus();
                    if (pSieuam_thainghen.Visible) sat_tucung.Focus();
                    if (pSieuam_thai3thang.Visible) satt_tucung.Focus();
                    if (pHiemmuon.Visible) hm_toantrang.Focus();
                    if (pSieuam_pk.Visible) sapk_tucung.Focus();
                    if (pCanlamsang.Visible) cls_ten.Focus();
                    if (pTheodoitimthai.Visible) tdt_conco.Focus();
                    if (pSieuammau.Visible) sam_slthai.Focus();
                }
            }
            else if (!bTiepdon)
            {
                MessageBox.Show("Không tìm thấy thông tin hành chính bệnh nhân!", DAL.AccessData.Msg); mabn1.Focus(); return;
            }
        }

        private void cboTuoi_Validated(object sender, EventArgs e)
        {
            try
            {
                if (cboTuoi.SelectedIndex == 0)
                    namsinh.Text = (int.Parse(DateTime.Now.Year.ToString()) - int.Parse(tuoi.Text)).ToString();
                else namsinh.Text = DateTime.Now.Year.ToString();
            }
            catch { }
        }

        private void butDonthuoc_Click(object sender, EventArgs e)
        {
            if (IsLoaded("frmDonthuoc")) return;
            if (l_maql == 0) return;
            ucDonThuoc f = new ucDonThuoc(l_maql, s_mabn, hoten.Text, namsinh.Text, ngaykham.Text + " " + giokham.Text, "Bs. " + cboBacsi.Text, chandoan.Text, mabs.Text, cboPhongkham.SelectedValue.ToString(), i_userid,s_mmyy);
            //fix:
            //f.MdiParent = this;
            f.Show();
            //fix:
            //f.ShowInTaskbar = false;
            f.Dock = DockStyle.Fill;
            f.BringToFront();
            f.Left = pForm.Left;
            f.Top = pForm.Top;
            f.Height = pForm.Height;
            f.Width = pForm.Width;
            pForm.SendToBack();
            pPhieukham.Visible = false;
            lChon.Text = "      ĐƠN THUỐC";
            butDonthuoc.BackColor = Color.Yellow;
        }
        private bool IsLoaded(string frm)
        {
            //Form[] afrm = this.MdiChildren;
            //foreach (Form f in afrm)
            //{
            //    if (f.Name.Equals(frm)) { f.Activate(); f.BringToFront(); pForm.SendToBack(); pPhieukham.Visible = false; return true; }
            //}
            return false;
        }

        private void QuanLyPhongKham_MdiChildActivate(object sender, EventArgs e)
        {
            pForm.BringToFront();
            pPhieukham.Visible = true;
            lChon.Text = "      THÔNG TIN KHÁM BỆNH";
            butDonthuoc.BackColor = Color.MintCream;
            butPttt.BackColor = Color.MintCream;
            butDiungthuoc.BackColor = Color.MintCream;
            butBaocao.BackColor = Color.MintCream;
            load_vv_mabn_chuakham();
        }

        private void mnuDmbacsi_Click(object sender, EventArgs e)
        {
            if (IsLoaded("frmDmbs")) return;
            ucDmBacSy f = new ucDmBacSy(m);
            //f.ShowDialog();
        }

        private void mnuDmphongkham_Click(object sender, EventArgs e)
        {
            if (IsLoaded("frmDmpk")) return;
            ucDmPhongKham f = new ucDmPhongKham(m);
            //f.ShowDialog();
        }

        private void mnuDmdoituong_Click(object sender, EventArgs e)
        {
            if (IsLoaded("frmDmdoituong")) return;
            ucDmDoiTuong f = new ucDmDoiTuong(m);
            //fix:
            //f.ShowDialog();
        }

        private void mnuXoathongtin_Click(object sender, EventArgs e)
        {

        }

        private void butDiungthuoc_Click(object sender, EventArgs e)
        {
            if (hoten.Text == "") return;
            ucDiUngThuoc f = new ucDiUngThuoc(s_mabn, hoten.Text, namsinh.Text);
            //fix:
            //f.ShowDialog();
        }

        private void butPttt_Click(object sender, EventArgs e)
        {
            if (IsLoaded("frmPttt")) return;
            if (hoten.Text == "")
            {
                MessageBox.Show("Vui lòng chọn bệnh nhân để thực hiện thủ thuật!");
                listDsbn.Focus();
                return;
            }
            frmPttt f = new frmPttt(m,l_maql, s_mabn, i_userid, l_maql > 0);
            //fix:
            //f.MdiParent = this;
            f.Show();
            f.ShowInTaskbar = false;
            f.Dock = DockStyle.Fill;
            f.BringToFront();
            f.Left = pForm.Left;
            f.Top = pForm.Top;
            f.Height = pForm.Height;
            f.Width = pForm.Width;
            pForm.SendToBack();
            pPhieukham.Visible = false;
            lChon.Text = "      PHẪU THUẬT - THỦ THUẬT";
        }

        private void butVienphi_Click(object sender, EventArgs e)
        {
            if (l_maql == 0) return;
            if (IsLoaded("frmChidinh")) return;
            ucChiDinh f = new ucChiDinh(l_maql, s_mabn, hoten.Text, namsinh.Text, cboGioitinh.Text, ngaykham.Text + " " + giokham.Text, "Bs. " + cboBacsi.Text, chandoan.Text, mabs.Text, cboPhongkham.SelectedValue.ToString(), i_userid, int.Parse(madoituong.Text));
            //fix:
            //f.MdiParent = this;
            f.Show();
            //f.ShowInTaskbar = false;
            f.Dock = DockStyle.Fill;
            f.BringToFront();
            f.Left = pForm.Left;
            f.Top = pForm.Top;
            f.Height = pForm.Height;
            f.Width = pForm.Width;
            pForm.SendToBack();
            pPhieukham.Visible = false;
            lChon.Text = "      CHỈ ĐỊNH DỊCH VỤ";
            butBaocao.BackColor = Color.Yellow;
        }

        private void mnuPhanquyen_Click(object sender, EventArgs e)
        {
            ucPhanQuyenSuDung f = new ucPhanQuyenSuDung(m);
            //fix:
            //f.ShowDialog();
        }

        private void cboPhongkham_SelectedIndexChanged(object sender, EventArgs e)
        {
            //try 
            //{
            //    DataRow rr = m.getrowbyid(dtpk, "makp='" + cboPhongkham.SelectedValue.ToString() + "'");
            //    if (rr != null)
            //    {
            //        mabs.Text = rr["mabs"].ToString();
            //        cboBacsi.SelectedValue = mabs.Text;
            //    }
            //}
            //catch { }
        }

        private void cboPhongkham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cboPhongkham.SelectedIndex == -1) cboPhongkham.SelectedIndex = 0;
                SendKeys.Send("{Tab}");
            }
        }

        private void logoffToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmLogin f = new frmLogin();
            f.ShowDialog(this);
            if (f.i_userid != 0)
            {
                s_makp_loc = f.s_makp.Trim(',').Replace(",", "','");
                s_makp = f.s_makp.Trim(',');
                s_menu = f.s_menu;
                i_userid = f.i_userid;
                this.Text = "NIKSOFT - Quản Lý Phòng Khám <" + f.s_hoten + ">";
                bTiepdon = s_menu.IndexOf("c01") != -1;
                listDsbn.Visible = true;
                if (bTiepdon)
                {
                    gCaclankham.Text = "Các lần đăng ký khám";
                    gDausinhton.Visible = false;
                    gCaclankham.Location = new Point(2, 2);
                    pTiepdon.Visible = true;
                    pDscho.Visible = false;
                    tVienphi.Visible = true; tTienich.Visible = false;
                    tDonthuoc.Visible = false; tPttt.Visible = false;
                }
                else
                {
                    gCaclankham.Text = "Các lần khám bệnh";
                    pTiepdon.Visible = false; tTienich.Visible = false;
                    gDausinhton.Visible = true; tPttt.Visible = true;
                    gKhambenh.Text = "Thông tin khám bệnh";
                    lChon.Text = "      THÔNG TIN KHÁM BỆNH";
                    butDiungthuoc.BackColor = Color.MintCream;
                    butDiungthuoc.ForeColor = Color.Black;
                }
                if (f.bAdmin)
                {
                    tTienich.Visible = true;
                    tVienphi.Visible = true;
                }
                if (!bTiepdon)
                {
                    pGiakham.Visible = true;
                    load_formnhap();
                }
                if (bTiepdon)
                {
                    pKhamvu.Visible = false; pKhampk.Visible = false; pGiakham.Visible = false; pTheodoitimthai.Visible = false; pSieuammau.Visible = false;
                    butIn.Visible = false; pKhamthai.Visible = false; pSieuam.Visible = false; pCanlamsang.Visible = false; pSieuam4D_12tuan.Visible = false; pSieuam_thai3thang.Visible = false;
                    lViettat.Visible = false; pSieuam_thainghen.Visible = false; pHiemmuon.Visible = false; pSieuam_pk.Visible = false; pEmpty.Visible = false;
                }
                else if (!bTiepdon) load_vv_mabn_chuakham();
                if (pDscho.Visible) pDscho.Focus();
                else mabn1.Focus();
                ngaykham.Text = m.ngayhienhanh_server;
                giokham.Text = m.giohienhanh_server;
                empty_text();
                cboGioitinh.SelectedIndex = 1;
                cbonn.SelectedIndex = 1;
                ena_doituong(false);
                ena_nuoc(false);
            }
            else Application.Exit();
        }

        private void vutrai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                if (vutrai.Text.IndexOf("\r\n") != -1)
                {
                    vutrai.Text = vutrai.Text.Trim();
                    vuphai.Focus();
                }
            }
        }

        private void vuphai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                if (vuphai.Text.IndexOf("\r\n") != -1)
                {
                    vuphai.Text = vuphai.Text.Trim();
                    xetnghiem.Focus();
                }
            }
        }

        private void xetnghiem_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                if (xetnghiem.Text.IndexOf("\r\n") != -1)
                {
                    xetnghiem.Text = xetnghiem.Text.Trim();
                    ketluan.Focus();
                }
            }
        }

        private void ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                if (ketluan.Text.IndexOf("\r\n") != -1)
                {
                    ketluan.Text = ketluan.Text.Trim();
                    giakham.Focus();
                }
            }
        }

        private void treeView1_Click(object sender, EventArgs e)
        {
            try
            {
                string ngay = treeView1.TopNode.Text;
                l_maql = m.get_maql_ngayvv(s_mabn, ngay.Substring(0,10));
                if (l_maql != 0)
                {
                    ngaykham.Text = ngay.Substring(0, 10);
                    giokham.Text = ngay.Substring(11);
                    load_vv_maql(true);
                    if (pKhamvu.Visible) load_khamvu();
                    if (pKhampk.Visible) load_khampk();
                    if (pKhamthai.Visible) load_khamthai();
                    if (pSieuam.Visible) load_sieuam4d();
                    if (pSieuam_thainghen.Visible) load_sieuam_thainghen();
                    if (pSieuam_thai3thang.Visible) load_sieuam_thai3thang();
                    if (pHiemmuon.Visible) load_hiemmuon();
                    if (pSieuam_pk.Visible) load_sieuam_pk();
                    if (pCanlamsang.Visible) load_canlamsang();
                    if (pTheodoitimthai.Visible) load_theodoitimthai();
                    if (pSieuammau.Visible) load_sieuammau();
                    if (pSieuam4D_12tuan.Visible) load_sieuam_4d_12tuan();
                }
            }
            catch { }
        }

        private void p1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void ketluan_pk_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void p1_Validated(object sender, EventArgs e)
        {
            p1.Text = p1.Text.PadLeft(2, '0');
        }

        private void p2_Validated(object sender, EventArgs e)
        {
            p2.Text = p2.Text.PadLeft(2, '0');
        }

        private void p3_Validated(object sender, EventArgs e)
        {
            p3.Text = p3.Text.PadLeft(2, '0');
        }

        private void p4_Validated(object sender, EventArgs e)
        {
            p4.Text = p4.Text.PadLeft(2, '0');
        }

        private void lydo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void butIn_Click(object sender, EventArgs e)
        {
            if (pCanlamsang.Visible) return;
            DataSet dst = new DataSet();
            string s_report = "", s_file = "";
            sql = "";
            if (pKhamvu.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_khamvu a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='"+s_mabn+"' and a.maql="+l_maql;
                dst = m.get_data(sql);
                s_file = "khamvu.xml";
                s_report = "ba_khamvu.rpt";
            }
            if (pKhamthai.Visible)
            {
                sql = "select to_char(a.dukiensinh,'dd/mm/yyyy') as dukiensinh,a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_khamthai a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_file = "khamthai.xml";
                s_report = "ba_khamthai.rpt";
            }
            if (pSieuam.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuam4d a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_file = "sieuam4d.xml";
                s_report = "ba_sieuam4d.rpt";
            }
            if (pSieuam_pk.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuampk a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_file = "sieuampk.xml";
                s_report = "ba_sieuampk.rpt";
            }
            if (pKhampk.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_khampk a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_file = "khampk.xml";
                s_report = "ba_khampk.rpt";
            }
            if (pSieuam_thainghen.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuamthai a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_file = "sieuamthai.xml";
                s_report = "ba_sieuamthai_nghen.rpt";
            }
            if (pSieuam_thai3thang.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuamthai_3thang a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_report = "ba_sieuamthai.rpt";
                s_file = "sieuamthai_3thang.xml";
            }
            if (pHiemmuon.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_hiemmuon a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_report = "ba_hiemmuon.rpt";
                s_file = "hiemmuon.xml";
            }
            if (pTheodoitimthai.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_theodoitimthai a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_report = "ba_theodoithai.rpt";
                s_file = "theodoitimthai.xml";
            }
            if (pSieuammau.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuammau a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_report = "ba_sieuammau.rpt";
                s_file = "sieuammau.xml";
            }
            if (pSieuam4D_12tuan.Visible)
            {
                sql = "select a.*,b.hoten,b.namsinh,b.phai,b.cholam,trim(b.diachi||' '||e.tenpxa||' '||d.tenquan||' '||c.tentt) as diachi from " + user + ".ba_sieuam4d_12tuan a, " + user + ".btdbn b, " + user + ".btdtt c, " + user + ".btdquan d, " + user + ".btdpxa e ";
                sql += " where a.mabn=b.mabn and b.matt=c.matt and b.maqu=d.maqu and b.maphuongxa=e.maphuongxa and a.mabn='" + s_mabn + "' and a.maql=" + l_maql;
                dst = m.get_data(sql);
                s_report = "ba_sieuam4d_12tuan.rpt";
                s_file = "sieuam4d_12tuan.xml";
            }
            if (sql == "") return;
            if (dst.Tables[0].Rows.Count == 0) return;
            DataColumn dc = new DataColumn("Image", typeof(byte[]));
            dst.Tables[0].Columns.Add(dc);
            if (File.Exists("..\\..\\..\\chuky\\" + bacsikham.SelectedValue.ToString() + ".bmp"))
            {
                FileStream fstr = new FileStream("..\\..\\..\\chuky\\" + bacsikham.SelectedValue.ToString() + ".bmp", FileMode.Open, FileAccess.Read);
                byte[] image = new byte[fstr.Length];
                fstr.Read(image, 0, System.Convert.ToInt32(fstr.Length));
                fstr.Close();
                foreach (DataRow r1 in dst.Tables[0].Rows) r1["Image"] = image;
            }
            dst.WriteXml(@"..\"+s_file, XmlWriteMode.WriteSchema);
            frmReport f = new frmReport(m, dst.Tables[0], "", s_report, "", "", "", "Bs. "+bacsikham.Text, ngaylamviec.Text.Substring(0,10), ngoithai.Text, "", "", "", "", "", "");
            f.Title = "Báo cáo";
            this.FormMain.OpenTab(f);
        }

        private void p1_kt_Validated(object sender, EventArgs e)
        {
            p1_kt.Text = p1_kt.Text.PadLeft(2, '0');
        }

        private void p2_kt_Validated(object sender, EventArgs e)
        {
            p2_kt.Text = p2_kt.Text.PadLeft(2, '0');
        }

        private void p3_kt_Validated(object sender, EventArgs e)
        {
            p3_kt.Text = p3_kt.Text.PadLeft(2, '0');
        }

        private void p4_kt_Validated(object sender, EventArgs e)
        {
            p4_kt.Text = p4_kt.Text.PadLeft(2, '0');
        }

        private void ngaykc_kt_Validated(object sender, EventArgs e)
        {
            //if (!m.bNgay(ngaykc_kt.Text))
            //{
            //    MessageBox.Show("Ngày kinh cuối không hợp lệ! Vui lòng nhập đúng định dạng : dd/MM/yyyy");
            //    ngaykc_kt.Focus();
            //    return;
            //}
        }

        private void ngaysinhdk_Validated(object sender, EventArgs e)
        {
            //if (!m.bNgay(ngaysinhdk.Text))
            //{
            //    MessageBox.Show("Ngày sinh dự kiến không hợp lệ! Vui lòng nhập đúng định dạng : dd/MM/yyyy");
            //    ngaysinhdk.Focus();
            //    return;
            //}
        }

        private void p1_kt_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskDigit(e);
        }
        private void sa_tucung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }
        private void sa_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }
        private void sa_chuvidau_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskDigit(e);
        }
        private void danhMụcGiáViệnPhíToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucGiaKham f = new ucGiaKham(m);
            //fix:
            //f.ShowDialog();
        }

        private void cboLoaikham_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboLoaikham.SelectedIndex == 0)
            {
                pEmpty.Visible = true;
                pEmpty.BringToFront();
                gKhambenh.Location = new Point(1, 250);
                gKhambenh.Height = 183 + (this.Height - 600);
            }
            else
            {
                pEmpty.Visible = false;
            }
            if (cboLoaikham.SelectedIndex != 0)
            {
                load_formnhap();
                if (!bTiepdon)
                {
                    if (pSieuam.Visible)
                    {
                        gKhambenh.Location = new Point(3, 178);
                        gKhambenh.Height = 255 + (this.Height - 600);
                        gKhambenh.BringToFront();
                        panel10.BringToFront();
                        bNut.BringToFront();
                    }
                    else
                    {
                        gKhambenh.Location = new Point(1, 250);
                        gKhambenh.Height = 183 + (this.Height - 600);
                    }
                }
            }
        }

        private void cboLoaikham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                //if (pKhamthai.Visible) p1_kt.Focus();
                //if (pKhampk.Visible) p1.Focus();
                //if (pKhamvu.Visible) vutrai.Focus();
                //if (pSieuam.Visible) sa_tucung.Focus();
                //if (pSieuam_thainghen.Visible) sat_tucung.Focus();
                //if (pSieuam_thai3thang.Visible) satt_tucung.Focus();
                //if (pHiemmuon.Visible) hm_toantrang.Focus();
                //if (pSieuam_pk.Visible) sapk_tucung.Focus();
                //if (pCanlamsang.Visible) cls_ten.Focus();
                //if (pTheodoitimthai.Visible) tdt_conco.Focus();
                //if (pSieuammau.Visible) sam_slthai.Focus();
                //if (pSieuam4D_12tuan.Visible) sa_12_tucung.Focus();
                SendKeys.Send("{Tab}{F4}");
            }
        }

        private void lViettat_Click(object sender, EventArgs e)
        {
            ucVietTat f = new ucVietTat(m);
            //fix:
            //f.ShowDialog();
            load_viettat();
        }
        private void get_tenviettat(object sender)
        {
            if (sender.GetType() == typeof(TextBox))
            {
                TextBox txt = (TextBox)sender;
                DataRow rr = m.getrowbyid(dtviettat, "tenvt='" + txt.Text.ToLower() + "'");
                if (rr != null)
                {
                    txt.Text = rr["diengiai"].ToString();
                }
            }
        }

        private void sat_tucung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void sat_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void hm_toantrang_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void hm_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }
        private void sapk_yeucau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void sapk_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void kinhcuoi_Validated(object sender, EventArgs e)
        {
            if (!m.bNgay(kinhcuoi.Text))
            {
                MessageBox.Show("Kinh cuối cùng không hợp lệ! Vui lòng nhập đúng định dạng : dd/MM/yyyy");
                kinhcuoi.Focus();
                return;
            }
        }

        private void lDichuyenlen_Click(object sender, EventArgs e)
        {
            
        }

        private void cls_ten_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void cls_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void tdt_conco_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void tdt_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void sam_dopler_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void sam_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void danhSáchKhámBệnhToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucDanhSachKham f = new ucDanhSachKham(m);
            //fix:
            //f.ShowDialog();
        }

        private void listDsbn_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                l_maql = long.Parse(listDsbn.SelectedValue.ToString());
                if (l_maql != 0)
                {
                    load_vv_maql(true);
                    load_mabn();
                }
                cboLoaikham.Focus();
                cboLoaikham.DroppedDown = true;
            }
            catch { }
        }

        private void timkiem_TextChanged(object sender, EventArgs e)
        {
            if (this.ActiveControl == timkiem && timkiem.Text != "Tìm kiếm")
            {
                CurrencyManager cm = (CurrencyManager)BindingContext[listDsbn.DataSource];
                DataView dv = (DataView)cm.List;
                dv.RowFilter = "hoten like '%" + timkiem.Text + "%'";
            }
        }

        private void timkiem_MouseDown(object sender, MouseEventArgs e)
        {
            if (timkiem.Text == "Tìm kiếm")
            {
                timkiem.Text = "";
            }
        }

        private void timkiem_Validated(object sender, EventArgs e)
        {
            if (timkiem.Text == "")
            {
                timkiem.Text = "Tìm kiếm";
            }
        }

        private void sam_slthai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void satt_tucung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void satt_dudoansinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void textBox5_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void sat_dudoansinh_Validated(object sender, EventArgs e)
        {
            if (!m.bNgay(sat_dudoansinh.Text))
            {
                MessageBox.Show("Ngày sinh không hợp lệ! Vui lòng nhập đúng định dạng : dd/MM/yyyy");
                sat_dudoansinh.Focus();
                return;
            }
        }

        private void sat_dudoansinh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void sa_12_tucung_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                SendKeys.Send("{Tab}");
            }
        }

        private void sa_12_ketluan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                get_tenviettat(sender);
                giakham.Focus();
            }
        }

        private void giakham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{Tab}");
            }
        }

        private void giakham_Validated(object sender, EventArgs e)
        {
            try 
            {
                if (giakham.Text.Trim().Length < 5)
                {
                    MessageBox.Show("Giá khám không được nhỏ hơn 10.000!",DAL.AccessData.Msg);
                    giakham.Focus(); return;
                }
                decimal dongia = decimal.Parse(giakham.Text);
                if (d_dongia != 0)
                {
                    if (dongia < d_dongia)
                    {
                        MessageBox.Show("Giá này nhỏ hơn giá đã nhập.\nGiá đã nhập trước của BN này là : " + d_dongia.ToString("###,###,##0"), DAL.AccessData.Msg);
                        giakham.Focus(); return;
                    }
                }
                giakham.Text = dongia.ToString("###,###,##0");
            }
            catch { }
        }

        private void ngoithai_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (ngoithai.SelectedIndex == -1) ngoithai.SelectedIndex = 0; 
                SendKeys.Send("{Tab}");
            }
        }

        private void sttkham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butLuu.Focus();
            }
        }

        private void sttkham_Validated(object sender, EventArgs e)
        {
            if (sttkham.Text.Trim() != "")
            {
                if (m.get_data("select stt from " + user + ".benhanpk where stt=" + int.Parse(sttkham.Text) + " and makp='"+cboPhongkham.SelectedValue.ToString()+"' and to_char(ngay,'dd/mm/yyyy')='" + ngaykham.Text + "'").Tables[0].Rows.Count > 0)
                {
                    MessageBox.Show("Số khám này đã được sử dụng!", DAL.AccessData.Msg);
                    sttkham.Focus(); return;
                }
            }
        }
        private bool kiemtra_thutien()
        {
            if (m.get_data("select mabn from " + user + ".v_chidinh where paid=1 and id=" + l_maql).Tables[0].Rows.Count > 0)
            {
                return true;
            }
            return false;
        }

        private void bacsikham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (pKhamthai.Visible) p1_kt.Focus();
                if (pKhampk.Visible) p1.Focus();
                if (pKhamvu.Visible) vutrai.Focus();
                if (pSieuam.Visible) sa_tucung.Focus();
                if (pSieuam_thainghen.Visible) sat_tucung.Focus();
                if (pSieuam_thai3thang.Visible) satt_tucung.Focus();
                if (pHiemmuon.Visible) hm_toantrang.Focus();
                if (pSieuam_pk.Visible) sapk_tucung.Focus();
                if (pCanlamsang.Visible) cls_ten.Focus();
                if (pTheodoitimthai.Visible) tdt_conco.Focus();
                if (pSieuammau.Visible) sam_slthai.Focus();
                if (pSieuam4D_12tuan.Visible) sa_12_tucung.Focus();
            }
        }

        private void tienthuoc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                butLuu.Focus();
            }
        }

        private void tienthuoc_Validated(object sender, EventArgs e)
        {
            try
            {
                decimal dongia =0;
                if (tienthuoc.Text.Trim()!= "")
                    dongia = decimal.Parse(tienthuoc.Text);
                tienthuoc.Text = dongia.ToString("###,###,##0");
            }
            catch { }
        }

        private void cboBacsi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cboBacsi.SelectedIndex == -1) cboBacsi.SelectedIndex = 0;
                SendKeys.Send("{Tab}");
            }
        }

        private void QuanLyPhongKham_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.ControlKey && e.KeyCode == Keys.O)
            {
                logoffToolStripMenuItem_Click(null, null); 
            }
        }
    }
}